forked from Anchor89/LeetCodeAnchor89
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctSubsequences.cpp
More file actions
33 lines (31 loc) · 821 Bytes
/
DistinctSubsequences.cpp
File metadata and controls
33 lines (31 loc) · 821 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
#include "LeetCode.h"
// Use only one dimension
class Solution {
public:
int numDistinct(string S, string T) {
vector<int> dp = vector<int>(T.size()+1, 0);
dp[0] = 1;
for (int i=1; i<=S.size(); i++) {
for (int j=T.size(); j>0; j--) {
if (S[i-1] == T[j-1]) {
dp[j] += dp[j-1];
}
}
}
return dp[T.size()];
}
};
class Solution {
public:
int numDistinct(string S, string T) {
int slen = S.size(), tlen = T.size();
vector<vector<int>> dp(tlen+1, vector<int>(slen+1, 0));
fill(dp[0].begin(), dp[0].end(), 1); // For empty T, the distinct subsequences is always 1.
for(int i=1; i<=tlen; i++) {
for (int j=1; j<=slen; j++) {
dp[i][j] = dp[i][j-1] + (T[i-1] == S[j-1]? dp[i-1][j-1]:0);
}
}
return dp[tlen][slen];
}
};