Distinct Subsequences
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.
Analysis
- 注意subsequences可以是不连续的子串
- 一般有两个字符串的情况要想到用二维数组dp[i][j], 表示string 1的前i的字符和string 2的前j个字符相比较 DP problem
- State: dp[i][j]: Number of distinct subsequences of t.substr(1....i) in s(1....j)
- Transfer: dp[i][j] = dp[i][j-1] + (dp[i-1][j-1] or 0)
- 如果s和t最后一个字母一样,那么出现的次数是s除去最后一个字母(不match最后一个)+同时除去最后一个字母(match最后一个)
- 如果s和t最后一个字母不一样,那么t在s中出现的次数和t在s除去最后一个字母出现的次数一样多
- 如何想到这样用DP见yuanbin分析
Note
易犯错误:s和t哪个是横左边,哪个做纵坐标容易犯错。这道题s做j,t做i,分析起来会比较方便。
Code
public class Solution {
public int numDistinct(String s, String t) {
int m = t.length();
int n = s.length();
if (m > n) return 0;
int[][] dp = new int[m+1][n+1];
for (int i=0; i<=n; i++) dp[0][i]=1;
for (int j=1; j<=n; j++){
for (int i=1; i<=m; i++){
dp[i][j] = dp[i][j-1] + (t.charAt(i-1)==s.charAt(j-1) ? dp[i-1][j-1] : 0);
}
}
return dp[m][n];
}
}
Note
- DP经常用在这种看似需要用DFS,实际上不需要求具体的变换过程,只需要count number的题目,用来大大减小时间复杂度