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

class Solution {
public:
    int numDistinct(string s, string t) {
        int ls = s.length(), lt = t.length(), i, j;
        vector<vector<int>> dp(lt+1, vector<int>(ls+1, 0));
        for(i = 0; i <= ls; i++)
            dp[0][i] = 1;
        for(i = 1; i <= lt; i++)
        {
            for(j = i; j <= ls; j++)
            {
                if(t[i-1] == s[j-1])
                {
                    dp[i][j] = dp[i-1][j-1]+dp[i][j-1];
                }
                else
                {
                    dp[i][j] = dp[i][j-1];
                }
            }
        }
        return dp[lt][ls];
    }
};

注意:

“bbb”和“”的匹配结果为1。所以第一行都为1。

时间: 2024-10-29 12:52:21

115. Distinct Subsequences *HARD* -- 字符串不连续匹配的相关文章

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

蠡口115. Distinct Subsequences

又是要对两个字符串一顿操作,返回T or F,典型的dp. 1.建立dp矩阵,确定状态变量:dp[i][j]表示the number of distinct subsequences of s[0:i] which equals t[0:j],就是s[0:i]与t[0:j]带入函数所得值,初值都设为0: 2.确定边界:当s为空字符时,如果t不为空字符,那么就不能通过对s里字符进行删减得到与t字符串一样的字符串,所以dp[0][j]应该为0 (对任意j>0).当t为空字符,当且仅当删除s中全部字符

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

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

115. Distinct Subsequences (String; 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

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