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

题意:如果某一个字符串str1是由另一个字符串str2中删除某些字符并保持其它字符在str2中的顺序得到的,

那么str1称为str2的子串

思路:

跟 Edit Distance 那道题类似

设 dp[i][j]表示 str1[0...j] 在 str2[0...i]中出现的次数,则

dp[i][j] = dp[i - 1][j] , if str1[j] != str2[i]

因为str1[j] != str2[i],所以只能删除掉str2[i]

dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j], if str1[j] == str2[i]

因为str1[j] == str2[i],所以可以选择保留 str2[i] 或删除掉它

实现的时候,可以只用一个一维滚动数组 dp[j] --> 只要哪一维在计算 i状态时只用到了 i-1的状态,就可以用滚动数组

-->这里好像错了,这里初始化的时候要将每个 dp[i][0] 设为1,但如果只用一个一维数组的话,就只能将 dp[0][0] 设为1

-->所以还是用二维数组

-->网上看到有人用一维数组也可以,是在 j 循环时,从后往前迭代。

复杂度:时间O(n^2),空间O(n^2)

int numDistinct(string S, string T) {
	vector<vector<int> > dp(S.size() + 1, vector<int>(T.size() + 1, 0));
	for(int i = 0; i <= S.size(); ++i) dp[i][0] = 1;
	for(int i = 1; i <= S.size(); ++i){
		for(int j = 1; j <= T.size(); ++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[S.size()][T.size()];
}
时间: 2024-10-26 16:41:15

Leetcode dp 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 之 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 di

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

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] 940. Distinct Subsequences II

Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may be large, return the answer modulo 10^9 + 7. Example 1: Input: "abc" Output: 7 Explanation: The 7 distinct subsequences are "a", "b

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】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