题目链接:https://leetcode.com/problems/distinct-subsequences/description/
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
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
.
过程一:
The idea is the following:
- we will build an array
mem
wheremem[i+1][j+1]
means thatS[0..j]
containsT[0..i]
that many times as distinct subsequences. Therefor the result will bemem[T.length()][S.length()]
. - we can build this array rows-by-rows:
- the first row must be filled with 1. That's because the empty string is a subsequence of any string but only 1 time. So
mem[0][j] = 1
for everyj
. So with this we not only make our lives easier, but we also return correct value ifT
is an empty string. - the first column of every rows except the first must be 0. This is because an empty string cannot contain a non-empty string as a substring -- the very first item of the array:
mem[0][0] = 1
, because an empty string contains the empty string 1 time.
So the matrix looks like this:
S 0123....j
T +----------+
|1111111111|
0 |0 |
1 |0 |
2 |0 |
. |0 |
. |0 |
i |0 |
From here we can easily fill the whole grid: for each (x, y)
, we check if S[x] == T[y]
we add the previous item and the previous item in the previous row, otherwise we copy the previous item in the same row. The reason is simple:
- if the current character in S doesn't equal to current character T, then we have the same number of distinct subsequences as we had without the new character.
- if the current character in S equal to the current character T, then the distinct number of subsequences: the number we had before plus the distinct number of subsequences we had with less longer T and less longer S.
An example:S: [acdabefbc]
and T: [ab]
first we check with a
:
* *
S = [acdabefbc]
mem[1] = [0111222222]
then we check with ab
:
* * ]
S = [acdabefbc]
mem[1] = [0111222222]
mem[2] = [0000022244]
And the result is 4, as the distinct subsequences are:
S = [a b ]
S = [a b ]
S = [ ab ]
S = [ a b ]
class Solution {
public:
int numDistinct(string s, string t) {
vector<vector<int>> mem(t.size()+1,vector<int>(s.size()+1,0));
for(int j=0;j<=s.size();j++)
mem[0][j]=1;
for(int i=0;i<t.size();i++)
{
for(int j=0;j<s.size();j++)
{
if(t[i]==s[j])
mem[i+1][j+1]=mem[i][j]+mem[i+1][j];
else
mem[i+1][j+1]=mem[i+1][j];
}
}
return mem[t.size()][s.size()];
}
};
过程二:
Well, a dynamic programming problem. Let's first define its state dp[i][j]
to be the number of distinct subsequences of t[0..i - 1]
in s[0..j - 1]
. Then we have the following state equations:
- General case 1:
dp[i][j] = dp[i][j - 1]
ift[i - 1] != s[j - 1]
; - General case 2:
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1]
ift[i - 1] == s[j - 1]
; - Boundary case 1:
dp[0][j] = 1
for allj
; - Boundary case 2:
dp[i][0] = 0
for all positivei
.
Now let's give brief explanations to the four equations above.
- If
t[i - 1] != s[j - 1]
, the distinct subsequences will not includes[j - 1]
and thus all the number of distinct subsequences will simply be those ins[0..j - 2]
, which corresponds todp[i][j - 1]
; - If
t[i - 1] == s[j - 1]
, the number of distinct subsequences include two parts: those withs[j - 1]
and those without; - An empty string will have exactly one subsequence in any string :-)
- Non-empty string will have no subsequences in an empty string.
class Solution {
public:
int numDistinct(string s, string t) {
int m = t.length(), n = s.length();
vector<vector<int>> dp(m + 1, vector<int> (n + 1, 0));
for (int j = 0; j <= n; j++) dp[0][j] = 1;
for (int j = 1; j <= n; j++)
for (int i = 1; i <= m; i++)
dp[i][j] = dp[i][j - 1] + (t[i - 1] == s[j - 1] ? dp[i - 1][j - 1] : 0);
return dp[m][n];
}
};
Notice that we keep the whole m*n
matrix simply for dp[i - 1][j - 1]
. So we can simply store that value in a single variable and further optimize the space complexity. The final code is as follows.
class Solution {
public:
int numDistinct(string s, string t) {
int m = t.length(), n = s.length();
vector<int> cur(m + 1, 0);
cur[0] = 1;
for (int j = 1; j <= n; j++) {
int pre = 1;
for (int i = 1; i <= m; i++) {
int temp = cur[i];
cur[i] = cur[i] + (t[i - 1] == s[j - 1] ? pre : 0);
pre = temp;
}
}
return cur[m];
}
};
题目给定两个字符串,选择只可以用删除字符的方法从第一个字符串变换到第二个字符串,求出一共有多少种变换方法;
又是动态规划思想应用的典型题目。
- 定义二维数组dp[i][j]为字符串s(0,i)变换到t(0,j)的变换方法。
- 如果S[i]==T[j],那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j]。意思是:如果当前S[i]==T[j],那么当前这个字母即可以保留也可以抛弃,所以变换方法等于保留这个字母的变换方法加上不用这个字母的变换方法。
- 如果S[i]!=T[i],那么dp[i][j] = dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。
- 递归公式中用到的dp[0][0] = 1,dp[i][0] = 0(把任意一个字符串变换为一个空串只有一个方法)
class Solution {
public:
int numDistinct(string s, string t) {
if(s.empty() || t.empty())
return 0;
else if(s.length()<t.length())
return 0;
else
{
int ls=s.length(),lt=t.length();
vector<vector<int>> dp(ls+1,vector<int>(lt+1,0));
dp[0][0]=1;
for(int i=1;i<=ls;i++)
{
dp[i][0]=1;
for(int j=1;j<=lt;j++)
{
if(s[i-1]!=t[j-1])
{
dp[i][j]=dp[i-1][j];
}
else
{
dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
}
}
}
return dp[ls][lt];
}
}
};