-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistinct-subsequences.cpp
More file actions
35 lines (29 loc) · 854 Bytes
/
Copy pathdistinct-subsequences.cpp
File metadata and controls
35 lines (29 loc) · 854 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
35
//
// Created by Chenguang Wang on 2024/2/25.
//
// https://leetcode.cn/problems/distinct-subsequences/description/
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int numDistinct(string s, string t) {
vector<vector<uint64_t>> dp(s.size() + 1, vector<uint64_t>(t.size() + 1));
for (int i = 0; i < s.size(); i++) {
dp[i][0] = 1;
}
for (int j = 1; j < t.size(); j++) {
dp[0][j] = 0;
}
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
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[s.size()][t.size()];
}
};