We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 49499ef commit 77d109dCopy full SHA for 77d109d
DistinctSubsequences/DistinctSubsequences.cpp
@@ -0,0 +1,26 @@
1
+class Solution {
2
+public:
3
+ int numDistinct(string S, string T) {
4
+ // Start typing your C/C++ solution below
5
+ // DO NOT write int main() function
6
+
7
+ int M = S.size();
8
+ int N = T.size();
9
10
+ if (M == 0 && N == 0)
11
+ return 1;
12
+ else if (M < N)
13
+ return 0;
14
15
+ int F[N+1];
16
+ memset(F, 0, sizeof(int)*(N+1));
17
+ F[0] = 1;
18
+ for (int i = 1; i <= M; i++) {
19
+ for (int j = N; j >= 1; j--) {
20
+ if (S[i-1] == T[j-1])
21
+ F[j] = F[j-1] + F[j];
22
+ }
23
24
+ return F[N];
25
26
+};
0 commit comments