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).
Example 1:
Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters)
rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
Example 2:
Input: S = "babgbag", T = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^
- Method: dp
- 当t长度为0时, 无论i长度为什么均为1,。
- 当s[i] != t[j]时,当前结果为前面出现的次数。dp[i][j] = dp[i - 1][j]。
- 当s[i] == t[j]时,结果为t长度不变dp[i - 1][j],前面成立的次数加上dp[i - 1][j - 1]。
class Solution {
public int numDistinct(String s, String t) {
int sLen = s.length();
int tLen = t.length();
int[][] dp = new int[sLen + 1][tLen + 1];
for(int i = 0; i < sLen; i++)
dp[i][0] = 1;
for(int i = 1; i <= sLen; i++){
for(int j = 1; j <= tLen; j++){
dp[i][j] += dp[i - 1][j];
if(s.charAt(i - 1) == t.charAt(j - 1)){
dp[i][j ] = dp[i - 1][j] + dp[i - 1][j - 1];
}
}
}
return dp[sLen][tLen];
}
}
- 延伸:s,t两个字符串,能否从s中移除一些字符变成t。
public static boolean stringChange(String s, String t){
int sLen = s.length();
int tLen = t.length();
boolean[][] dp = new boolean[sLen + 1][tLen + 1];
for(int i = 0; i <= sLen; i++)
dp[i][0] = true;
for(int i = 1; i <= sLen; i++){
for(int j = 1; j <= tLen; j++){
if(s.charAt(i - 1) != t.charAt(j - 1)){
dp[i][j] |= dp[i - 1][j]; //在t不变的情况下,如果前面成功了就一直成功。
}else{
dp[i][j] = dp[i - 1][j] | dp[i - 1][j - 1];//如果相同仍然是前面成功就一直成功,但是要多加一个条件,就是S,T长度各减一的情况下如果成功,当前也是成功的,所以同样成功。
}
}
}
for(boolean[] bs : dp){
for(boolean b : bs)
System.out.print(b + " ");
System.out.println();
}
return dp[sLen][tLen];
}
class Solution {
public int numDistinct(String s, String t) {
if(s == null || t == null) return 0;
int sLen = s.length(), tLen = t.length();
int[][] dp = new int[sLen + 1][tLen + 1];
dp[0][0] = 1;
for(int i = 1; i <= sLen; i++)
dp[i][0] = 1;
char[] sArr = s.toCharArray();
char[] tArr = t.toCharArray();
for(int i = 1; i <= sLen; i++){
char c1 = sArr[i - 1];
for(int j = 1; j <= tLen; j++){
char c2 = tArr[j - 1];
dp[i][j] = dp[i - 1][j];
if(c1 == c2) dp[i][j] += dp[i - 1][j - 1];
}
}
return dp[sLen][tLen];
}
}
- Method 1: dp[i][j]: this dp matrix saves the ways of reaching i index of target with j index of source string.
- Initialization: dp[0][0] = 1, nothing changed, 1 way.
- dp[0][1: sLen] = 1: we need to delete all characters so there is only one way.
- dp[i][i] = dp[i][j - 1]: Remove current character to get i + (if same character for target and source) dp[i - 1][j - 1]
class Solution { public int numDistinct(String s, String t) { char[] sArr = s.toCharArray(), tArr = t.toCharArray(); int sLen = sArr.length, tLen = tArr.length; int[][] dp = new int[tLen + 1][sLen + 1]; dp[0][0] = 1; for(int i = 1; i <= sLen; i++) dp[0][i] = dp[0][i - 1]; for(int i = 1; i <= tLen; i++){ for(int j = 1; j <= sLen; j++){ dp[i][j] = dp[i][j - 1]; if(sArr[j - 1] == tArr[i - 1]){ dp[i][j] += dp[i - 1][j - 1]; } } } return dp[tLen][sLen]; } }