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

[Solution]

Let W(i, j) stand for the number of subsequences of S(0, i) in T(0, j). If S.charAt(i) == T.charAt(j), W(i, j) = W(i-1, j-1) + W(i-1,j); Otherwise, W(i, j) = W(i-1,j).

 1 int numDistinct(string S, string T)
 2     {
 3         int m = S.size(), n = T.size(), i, j, result;
 4         int **table = new int* [m + 1];
 5         for (i = 0; i <= m; i++)
 6             table[i] = new int[n + 1];
 7
 8         for (j = 0; j <= n; j++) // S is empty
 9             table[0][j] = 0;
10         for (i = 0; i <= m; i++) // T is empty
11             table[i][0] = 1;
12
13         for (i = 1; i <= m; i++)
14         {
15             for (j = 1; j <= n; j++)
16             {
17                 if (S[i - 1] != T[j - 1])
18                     table[i][j] = table[i - 1][j];
19                 else
20                     table[i][j] = table[i - 1][j] + table[i - 1][j - 1];
21             }
22         }
23         result = table[m][n];
24
25         for (i = 0; i <= m; i++)
26             delete[] table[i];
27         delete[] table;
28
29         return result;
30     }
时间: 2024-12-26 20:18:37

leetcode 115. Distinct Subsequences的相关文章

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 115 Distinct Subsequences

problem:https://leetcode.com/problems/distinct-subsequences/ 字符匹配类型题目. class Solution { public: vector<vector<int>> dp; int numDistinct(string& s, string& t, int i, int j) { if(j == t.size()) return 1; if(i == s.size()) return 0; if(dp

【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 ]

115 Distinct Subsequences

115 Distinct Subsequences 这道题是dp, 我定义的dp[i][j] 为用t[:i] 和s[:j] 的disitinct subsequence排列数 class Solution: # @param {string} s # @param {string} t # @return {integer} def numDistinct(self, s, t): m, n = len(t), len(s) if m == 0 or n == 0: return 0 dp,cu

115. Distinct Subsequences(js)

115. Distinct Subsequences 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 charac

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 Sequence

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