-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path115.distinct-subsequences.cpp
More file actions
61 lines (58 loc) · 1.45 KB
/
115.distinct-subsequences.cpp
File metadata and controls
61 lines (58 loc) · 1.45 KB
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
53
54
55
56
57
58
59
60
61
// Tag: String, Dynamic Programming
// Time: O(NM)
// Space: O(NM)
// Ref: -
// Note: -
// Given two strings s and t, return the number of distinct subsequences of s which equals t.
// The test cases are generated so that the answer fits on a 32-bit signed integer.
//
// Example 1:
//
// Input: s = "rabbbit", t = "rabbit"
// Output: 3
// Explanation:
// As shown below, there are 3 ways you can generate "rabbit" from s.
// rabbbit
// rabbbit
// rabbbit
//
// Example 2:
//
// Input: s = "babgbag", t = "bag"
// Output: 5
// Explanation:
// As shown below, there are 5 ways you can generate "bag" from s.
// babgbag
// babgbag
// babgbag
// babgbag
// babgbag
//
// Constraints:
//
// 1 <= s.length, t.length <= 1000
// s and t consist of English letters.
//
//
class Solution {
public:
int numDistinct(string s, string t) {
int n = s.size();
int m = t.size();
vector<vector<long>> dp(n + 1, vector<long>(m + 1, 0));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <=m; j++) {
if (i == 0 || j == 0) {
dp[i][j] = (j == 0) ? 1: 0;
} else {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
}
return dp[n][m];
}
};