-
Notifications
You must be signed in to change notification settings - Fork 213
/
DistinctSubsequences.h
53 lines (51 loc) · 1.62 KB
/
DistinctSubsequences.h
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
/*
Author: Annie Kim, [email protected] : King, [email protected]
Date: May 16, 2013
Update: Oct 07, 2014
Problem: Distinct Subsequences
Difficulty: Easy
Source: http://leetcode.com/onlinejudge#question_115
Notes:
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting
some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
Solution: dp.
*/
class Solution {
public:
int numDistinct(string S, string T) {
return numDistinct_2(S, T);
}
int numDistinct_1(string S, string T) {
int M = S.size();
int N = T.size();
vector<vector<int> > dp(M+1, vector<int>(N+1));
for (int j = 0; j <= N; ++j) {
dp[0][j] = 0;
}
for (int i = 0; i <= M; ++i) {
dp[i][0] = 1;
}
for (int i = 1; i <= M; ++i) {
for (int j = 1; j <= N; ++j) {
dp[i][j] = dp[i-1][j] + (S[i-1] == T[j-1] ? dp[i-1][j-1] : 0);
}
}
return dp[M][N];
}
int numDistinct_2(string S, string T) {
int M = S.size();
int N = T.size();
vector<int> dp(N + 1);
dp[0] = 1;
for (int i = 1; i <= M; ++i) {
for (int j = N; j >=1; --j) {
dp[j] = dp[j] + (S[i-1] == T[j-1] ? dp[j-1] : 0);
}
}
return dp[N];
}
};