-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathDistinctSubsequences.cpp
39 lines (32 loc) · 1.04 KB
/
DistinctSubsequences.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
/*
Given two sequences S, T, count number of unique ways in sequence S, to form a subsequence that is identical to the sequence T.
Subsequence : 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).
Example :
S = "rabbbit"
T = "rabbit"
Return 3. And the formations as follows:
S1= "ra_bbit"
S2= "rab_bit"
S3="rabb_it"
"_" marks the removed character.
LINK: https://www.interviewbit.com/problems/distinct-subsequences/
*/
int Solution::numDistinct(string A, string B)
{
int na = A.length();
int nb = B.length();
int dp[na+1][nb+1];
memset(dp,0,sizeof(dp));
for(int i=0;i<na;i++)
dp[i][0] = 1;
for(int i=1;i<=na;i++)
{
for(int j=1;j<=nb;j++)
{
dp[i][j] = dp[i-1][j];
if(A[i-1]==B[j-1])
dp[i][j] += dp[i-1][j-1];
}
}
return dp[na][nb];
}