Distinct Subsequences
Total Accepted: 6119 Total Submissions: 25615 My Submissions
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.
题目大意为求子串T在父串S中出现的次数。
好像可以DFS和BFS?应该会超时吧,而且DFS和BFS规律性不强,不太好写。
DP吧。重点是状态转移方程。
假设dp[i][j]表示i在j中出现的次数
1、i为0时,dp[i][j] = 1,因为空串可以匹配任何字符串,包括空串。
2、j为0时,dp[i][j] = 0,此时i >= 1。
3、i和j都不为0时,如果T.charAt(i) == S.charAt(j),那么dp[i][j] = dp[i][j-1] + dp[i-1][j-1]。否则dp[i][j] = dp[i][j-1]。
Total Accepted: 6119 Total Submissions: 25615 My Submissions
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.
题目大意为求子串T在父串S中出现的次数。
好像可以DFS和BFS?应该会超时吧,而且DFS和BFS规律性不强,不太好写。
DP吧。重点是状态转移方程。
假设dp[i][j]表示i在j中出现的次数
1、i为0时,dp[i][j] = 1,因为空串可以匹配任何字符串,包括空串。
2、j为0时,dp[i][j] = 0,此时i >= 1。
3、i和j都不为0时,如果T.charAt(i) == S.charAt(j),那么dp[i][j] = dp[i][j-1] + dp[i-1][j-1]。否则dp[i][j] = dp[i][j-1]。
Java AC
public class Solution {
public int numDistinct(String S, String T) {
int len1 = S.length();
int len2 = T.length();
if(len1 < len2){
return 0;
}
int dp[][] = new int[len2+1][len1+1];
for(int i = 0; i <= len2; i++){
dp[i][0] = 0;
}
for(int j = 0; j <= len1; j++){
dp[0][j] = 1;
}
for(int i = 1; i <= len2; i++){
for(int j = 1; j <= len1; j++){
if(T.charAt(i-1) == S.charAt(j-1)){
dp[i][j] = dp[i][j-1] + dp[i-1][j-1];
}else{
dp[i][j] = dp[i][j-1];
}
}
}
return dp[len2][len1];
}
}