You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*dp[i][j] denote the number of distinct subsequences starting with 'i' and end with 'j'
2
+
Here's the dp transition:
3
+
Case '0':
4
+
If the current character is '0', since we are counting distinct subsequences and there shouldn't be any leading zeros in the subsequences, we set dp[0][0] = 1, ex: string "000000" only have a subsequence '0'
5
+
dp[1][0] = dp[1][0] + dp[1][1] because it will be the number of ways that we add a '0' to a subsequence starting with '1' and ends with either '0' or '1'
6
+
7
+
Case '1':
8
+
If the current character is '1', since we can't have leading zeros, dp[0][1] is always 0 ("0001" is not a valid subsequence)
9
+
The transition will be dp[1][1] = dp[1][0] + dp[1][1] + 1. As we can see, dp[1][0] + dp[1][1] part is same as the case ending with zero. Since we append a '1' to every subsequence dp[1][0] and dp[1][1], we are missing the subsequence "1" with length 1; therefore, we add a one to it.
10
+
11
+
Note that all calculation above should be done with modulo.
0 commit comments