Dynamic Programming Series
Dynamic Programming Series
Day 10
Distinct Sub sequences
(Leetcode)
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example :
S = "rabbbit"
T = "rabit"
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example :
S = "rabbbit"
T = "rabit"
1. rabbbit
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example :
S = "rabbbit"
T = "rabit"
1. rabbbit
2. rabbbit
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example :
S = "rabbbit"
T = "rabit"
1. rabbbit
2. rabbbit
3. rabbbit
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example 1:
S = "rabbbit"
T = "rabit"
1. rabbbit
2. rabbbit
3. rabbbit
Example 2:
S = "cbabacbacab"
T = "abb"
1. cbabacbacab
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example 1:
S = "rabbbit"
T = "rabit"
1. rabbbit
2. rabbbit
3. rabbbit
Example 2:
S = "cbabacbacab"
T = "abb"
1. cbabacbacab
2. cbabacbacab
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example 1:
S = "rabbbit"
T = "rabit"
1. rabbbit
2. rabbbit
3. rabbbit
Example 2:
S = "cbabacbacab"
T = "abb"
1. cbabacbacab
2. cbabacbacab
3. cbabacbacab
Problem Description
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
Example 1:
S = "rabbbit"
T = "rabit"
1. rabbbit
2. rabbbit
3. rabbbit
Example 2:
S = "cbabacbacab"
T = "abb"
1. cbabacbacab
2. cbabacbacab
3. cbabacbacab
4. cbabacbacab
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now reduce problem to consider T = "a" only.
Now How many Sub sequences you can get from S?
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now reduce problem to consider T = "a" only.
Now How many Sub sequences you can get from S for this case?
| c | b | a | b | a | c | b | a | c | a |
| b |
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 3 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 4 |
| b |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 4 |
| b |
| 4 |
T = "a"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now reduce problem to consider T = "a" only.
Now How many Sub sequences you can get from S?
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 4 |
| b |
| 4 |
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now reduce problem to consider T = "ab" only.
Now How many Sub sequences you can get from S?
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| b |
| 4 |
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
Here b matches but we don't have any "a" that has matched earlier.
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
Here b matches and we know that there is only one time "a" has matched previously
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| b |
| 4 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| b |
| 4 |
| 7 |
T = "ab"
T = "a"
T = "ab"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now reduce problem to consider T = "ab" only.
Now How many Sub sequences you can get from S?
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| b |
| 4 |
| 7 |
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now we will consider the whole T as it is.
Now How many Sub sequences you can get from S?
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| b |
| 4 |
| 7 |
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
Here b matches but we know there is no ab previously that is matched.
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
Here b matches but we know there is no ab previously that is matched.
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| b |
| 4 |
| 7 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| b |
| 4 |
| 7 |
| 4 |
T = "a"
T = "ab"
T = "abb"
How To approach this problem?
Let's Do some observations first by taking second example.
S = "cbabacbacab"
T = "abb"
Now reduce problem to consider T = "abb" only.
Now How many Sub sequences you can get from S?
| c | b | a | b | a | c | b | a | c | a |
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 3 | 2 | 4 |
| 0 | 0 | 0 | 1 | 1 | 1 | 3 | 3 | 3 | 3 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| b |
| 4 |
| 7 |
| 4 |
T = "a"
T = "ab"
T = "abb"
Till Now we have seen how we can approach the problem.
Observations:
1.Number of subsequences that we can create from S of T[0...i] is equal to number of subsequences we have of T[0...i-1] concatenated to number of times of which T[i] occurs in S
Till Now we have seen how we can approach the problem.
Now we will create a DP matrix which will be represented as:
Fill DP[0][j] = 1 if s[j] == t[0]
Till Now we have seen how we can approach the problem.
Now we will create a DP matrix which will be represented as:
Fill DP[0][j] = 1 if s[j] == t[0]
Now there will be two conditions:
1. when s[j-1] == t[i-1] DP[i][j] = DP[i][j-1] + DP[i-1][j]
2. when s[j-1] != t[i-1] DP[i][j] = DP[i][j-1]
Let us code the problem.
int numDistinct(string s, string t) {
int n = s.size();
int m = t.size();
vector <vector<long long>> dp(m+1, vector <long long>(n+1));
for (int i = 1;i <= n;i++)
{
if (t[0] == s[i-1])
dp[1][i] = dp[1][i-1] + 1;
else
dp[1][i] = dp[1][i-1];
}
for (int i = 2;i <= m;i++)
{
for (int j = 1; j <= n;j++)
{
if (t[i-1] == s[j-1])
dp[i][j] = dp[i][j-1] + dp[i-1][j-1];
else
dp[i][j] = dp[i][j-1];
}
}
return dp[m][n];
}So that's It for Day 10
So that's It for Day 10
Concepts that we have learnt
So that's It for Day 10
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. DP in strings
So that's It for Day 10
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. DP in Strings.
2. Use of optimal Substructure with memoization
So that's It for Day 10
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. DP in Strings
2. Use of optimal Substructure with memoization
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.
Distinct Subsequences
By gauravsahu
Distinct Subsequences
- 45