`

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

public class Solution {
    public int numDistinct(String s, String t) {
        if (s == null)
            return 0;
        if (t == null || t.length() == 0)
            return 1;
        if (s.length() == 0)
            return 0;
        int lenS = s.length();
        int lenT = t.length();
        int[][] dp = new int[2][lenT + 1];
        dp[0][0] = 1;
        int lastLenS = 0;
        int currLenS = 1;
        for (int i = 1; i <= lenS; i++) {
            currLenS = 1 - lastLenS;
            dp[currLenS][0] = 1;
            for (int j = 1; j <= lenT; j++) {
                dp[currLenS][j] = dp[lastLenS][j];
                if (s.charAt(i - 1) == t.charAt(j - 1))
                    dp[currLenS][j] += dp[lastLenS][j-1];
            }
            lastLenS = currLenS;
        }
        return dp[currLenS][lenT];
    }
    
    // dp[i][j] = dp[i - 1][j] + (s[i - 1] == t[j - 1] ? dp[i - 1][j - 1] : 0);
    // dp[i][j]表示s的前i字符构成的子串能匹配t中前j个字符构成的子串的子序列数
    // 不管s和t当前考察位置处的字符是否相等,dp[i][j]至少有dp[i-1][j]
    // 如果当前考察位置处两字符相等,则再加上dp[i-1][j-1]的数目
    public int numDistinct1(String s, String t) {
        if (s == null)
            return 0;
        if (t == null || t.length() == 0)
            return 1;
        if (s.length() == 0)
            return 0;
        int lenS = s.length();
        int lenT = t.length();
        int[][] dp = new int[lenS + 1][lenT + 1];
        dp[0][0] = 1;
        for (int i = 1; i <= lenS; i++) {
            dp[i][0] = 1;
            for (int j = 1; j <= lenT; j++) {
                dp[i][j] = dp[i - 1][j];
                if (s.charAt(i - 1) == t.charAt(j - 1))
                    dp[i][j] += dp[i - 1][j - 1];
            }
        }
        return dp[lenS][lenT];
    }
}
分享到:
评论

相关推荐

    js-leetcode题解之115-distinct-subsequences.js

    在探讨js-leetcode题解之115-distinct-subsequences.js这一主题时,我们需要深入理解问题的背景、算法的核心原理,以及JavaScript语言在解决这个问题中的具体应用。 ### 问题背景 题目编号115,即“不同的子序列”...

    python-leetcode题解之115-Distinct-Subsequences

    在处理字符串匹配和子序列问题时,LeetCode上的题目“115. 不同的子序列”是一个经典的动态规划问题。给定两个字符串s和t,要求我们计算s中有多少个不同的子序列等于t。一个子序列可以不连续,但必须保持原有的顺序...

    leetcode1-240题中文题解,md格式,java

    10. leetcode-115-Distinct-Subsequences.md:第115题,不同的子序列,涉及到动态规划和字符串处理。 这些题目覆盖了各种算法和数据结构,包括字符串处理、动态规划、树结构遍历、数组处理、位运算、深度优先搜索、...

    java-leetcode题解之Distinct Subsequences.java

    题目“Distinct Subsequences”要求我们计算在给定字符串S中不同的子序列数量。 为了解决这个题,我们首先需要理解“子序列”的定义。在字符串理论中,子序列是由另一个序列删除一些元素(也可能不删除)且不改变...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth ...distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca

    javalruleetcode-learn-algorithms::laptop:Java实现的各种算法题解

    Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态规划-Longest Valid Parentheses](./leetcode/动态规划-Longest Valid Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./...

    115.Distinct Subsequences不同的子序列【LeetCode单题讲解系列】

    115.Distinct_Subsequences不同的子序列【LeetCode单题讲解系列】

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions ...Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar

    leetcode卡-ojs:ojs

    leetcode卡 OJ 题目汇总 记录下自己做过的oj题目, 也记录下自己对算法以及编程的理解与...Subsequences两种解法解题失败 [] 2014-06-14-13:00 ~ 2014-06-14-14:40 Interleaving String动态规划解决, Recovering BST失败

    javalruleetcode-magician:java学习

    java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: ...Subsequences] () [Rotate Image] () [Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

    2018年最新Google Onsite面经总结1

    Distinct Subsequences、152. Maximum Product Subarray等。这类题目通常要求读者使用动态规划来解决问题,例如使用DP数组来记录状态、使用递归来计算结果等。 6. 排序和查找类题目 排序和查找类题目是LeetCode原...

    3、动态规划必练题(含解法).pdf

    10. **Distinct Subsequences** (不同的子序列) 统计一个字符串所有非空子序列在另一个字符串中出现的次数。动态规划方法,考虑当前字符是否出现在子序列中。 11. **Largest Rectangle in Histogram** (柱状图中...

Global site tag (gtag.js) - Google Analytics