leetcode-distinct sequences

Solution: when see question about two strings , DP should be considered first.

We can abstract this question to calculate appear times for string T with length i in string S with length j, which can be represented by numbers[i][j], then through observation and thinking , we can know for numbers[i][j] it should at least equal the numbers[i][j-1] and if T.charAt(i)==S.charAt(j) , numbers[i][j] should also be add numbers[i-1][j-1]

 1 class Solution
 2 {
 3 public:
 4     int numDistinct(string S, string T) {
 5         int sLen = S.length(); int tLen = T.length();
 6         vector<vector<int>> dp(sLen+1,vector<int>(tLen+1));//dp[i][j]表示对应S前i个和T前j个字符的子问题。
 7         for (int i = 0; i <= sLen; i++) dp[i][0] = 1;
 8         for (int i = 1; i <= sLen; i++)
 9         {
10             for (int j = 1; j <= tLen; j++)
11             {
12                 if (S[i - 1] == T[j - 1])
13                 {
14                     dp[i][j] = dp[i - 1][j] + dp[i-1][j-1];
15                 }
16                 else
17                 {
18                     dp[i][j] = dp[i-1][j];
19                 }
20             }
21         }
22         return dp[sLen][tLen];
23     }
24
25 };
26 int main()
27 {
28     Solution s;
29     string strS("b");
30     string strT("a");
31     cout << s.numDistinct(strS, strT) << endl;
32     return 0;
33 }

http://rleetcode.blogspot.com/2014/01/distinct-subsequences-java.html

时间: 2024-12-15 12:44:27

leetcode-distinct sequences的相关文章

LeetCode: Distinct Subsequences

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

LeetCode: Distinct Subsequences [115]

[题目] 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

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 relative

[LeetCode] Distinct Subsequences(DP)

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 &quot;Distinct Subsequences&quot; - TO BE REVISED

A classic and representative DP problem. To be revised later. Quite a few interesting details to think about. class Solution { public: int numDistinct(string S, string T) { int lenS = S.length(); int lenT = T.length(); if (lenS < lenT) return 0; if (

[leetcode]Distinct Subsequences @ Python

原题地址:https://oj.leetcode.com/problems/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

[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 relative

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] Distinct Subsequences [29]

题目 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 relati

[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 relative