forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct-subsequences.cpp
55 lines (48 loc) · 1.63 KB
/
distinct-subsequences.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Time: O(m * n)
// Space: O(m)
// DP with rolling window.
class Solution {
public:
/**
* @param S, T: Two string.
* @return: Count the number of distinct subsequences
*/
int numDistinct(string &S, string &T) {
vector<vector<int>> ways(2, vector<int>(T.length() + 1, 0));
// ways[i][j] means number of distinct subsequence of S[0, i] with T[0, j]
ways[0][0] = 1;
for (int i = 0; i < S.length(); ++i) {
ways[(i + 1) % 2][0] = ways[i % 2][0];
for (int j = 0; j < T.length(); ++j) {
ways[(i + 1) % 2][j + 1] = S[i] == T[j]?
ways[i % 2][j + 1] + ways[i % 2][j] :
ways[i % 2][j + 1];
}
}
return ways[S.length() % 2][T.length()];
}
};
// Time: O(m * n)
// Space: O(m * n)
// DP
class Solution2 {
public:
/**
* @param S, T: Two string.
* @return: Count the number of distinct subsequences
*/
int numDistinct(string &S, string &T) {
vector<vector<int>> ways(S.length() + 1, vector<int>(T.length() + 1, 0));
// ways[i][j] means number of distinct subsequence of S[0, i] with T[0, j]
ways[0][0] = 1;
for (int i = 0; i < S.length(); ++i) {
ways[i + 1][0] = ways[i][0];
for (int j = 0; j < T.length(); ++j) {
ways[i + 1][j + 1] = S[i] == T[j]?
ways[i][j + 1] + ways[i][j] :
ways[i][j + 1];
}
}
return ways[S.length()][T.length()];
}
};