-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinct Subsequences.cpp
More file actions
34 lines (31 loc) · 874 Bytes
/
Distinct Subsequences.cpp
File metadata and controls
34 lines (31 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// https://oj.leetcode.com/problems/distinct-subsequences/
// Reference:
// http://fisherlei.blogspot.com/2012/12/leetcode-distinct-subsequences_19.html
class Solution {
public:
int numDistinct(string S, string T) {
if (S.size() == 0) {
return 0;
}
if (T.size() == 0) {
return 1;
}
int opt[T.size() + 1];
for (size_t i = 0; i <= T.size(); i++) {
opt[i] = 0;
}
/*
* Initial step: if S[0] == T[0]:
* opt[1] += opt[0]
*/
opt[0] = 1;
for (size_t i = 1; i <= S.size(); i++) {
for (size_t j = T.size(); j >= 1; j--) {
if (S[i-1] == T[j-1]) {
opt[j] += opt[j-1];
}
}
}
return opt[T.size()];
}
};