leetcode 之 Distinct Subsequences

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.

分析:题目是要求把S中去掉若干字符后得到T,这样的去法有多少种。

动态规划,设dp[i][j]表示S前i个字符通过删除字符得到T前j个字符的转换方法,则

(1)若S[i]==T[j],说明把S[i]、T[j]分别加入S[0~i-1]和T[0~j-1]可以完全复制dp[i-1][j-1]种转换方式。另外,S[0~i-1]本身(无需加入S[i])也能以dp[i-1][j]种方式转换为T[0~j],当然可能有0种,比如S[0~i-1]中根本不包含T[j],但由于i
>= j,所有S[0~i-1]可能包含T[0~j],所以有

dp[i][j] = dp[i-1][j-1] + dp[i-1][j];

(2)若S[i]!=T[j],说明S[i]无法用于转换为T[0~j],所以把S[i]加入进行转换的方法和不加入是重复的,所以T[j]需要从S[0~i-1]中获取,因此S[0~i]与S[0~i-1]无异,即dp[i][j] = dp[i-1][j];

初始化为:dp[i][0]设为1,即任何长度的S,如果转换为空串,那就只有删除全部字符这1中方式。

具体代码如下:

class Solution {
public:
    int numDistinct(string S, string T)
    {
    	int length1 = S.size(),length2 = T.size(),i,j;
    	vector< vector<int> > dp(length1+1);
    	for (i = 0;i <= length1;++i)
    	{
    		vector<int> tmp(length2+1,0);
    		dp[i] = tmp;
    	}
    	for (i = 0;i <= length1;i++)dp[i][0] = 1;//初始化
    	for (i = 1;i <= length1;i++)
    	{
    		for (j = 1;j <= length2;j++)
    		{
    			if(S[i-1] == T[j-1])dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
    			else dp[i][j] = dp[i-1][j];
    		}
    	}
    	return dp[length1][length2];
    }
};
时间: 2024-12-18 04:29:01

leetcode 之 Distinct Subsequences的相关文章

【leetcode】Distinct Subsequences

问题:给定两个字符串S,T,对于S,可以删除其中的任意多个(包括0)字符,使其得到T.问有多少种删法可以得到T. 举例分析: S:ababa T: aba dp[i][j] : 表示 S 从0 ~ i - 1,T从0~j - 1,所得到的方法数.i,j 表示长度. 初始条件:dp[i][0] = 1,T为空串,而空串总是任意串的字串.即,将S串的所有字符都删掉,就得到T. 状态转移方程: dp[ i ] [ j ] = dp[ i - 1] [ j - 1] + dp[ i - 1] [ j ]

leetcode 115. 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

Leetcode dp Distinct Subsequences

Distinct Subsequences Total Accepted: 15484 Total Submissions: 62324My 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 strin

leetcode 115 Distinct Subsequences ----- java

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

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution 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

【leetcode】Distinct Subsequences(hard)

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

Java for LeetCode 115 Distinct Subsequences【HARD】

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

[LeetCode][Java] 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 relat

【LeetCode】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 rela