Distinct Subsequences Leetcode

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.

初始化nums[s.length()+1][t.length() +1] 数组,

nums[i][j]表示s的前i个字符串中选取t的前j个字符,有多少种方案,

如果t为空,从一个字符串里选出来空串,只有一种方案,就是什么都不选。

如果s为空,从空串里选字符串,方案数为0,由于数组不赋值默认就是0,所以不需要再次初始化这部分。

如果s的第i个字符和t的第j个字符不相等,那么从s的前i个字符里选t的前j个字符的方案就等于s的前i -1个字符里选t的前j个字符的方案数。

如果s的第i个字符和t的第j个字符相等,那么从s的前i个字符里选t的前j个字符的方案就等于s的前i -1个字符里选t的前j个字符的方案数加s的前i -1个字符里选t的前j-1个字符的方案数

public class Solution {
    public int numDistinct(String s, String t) {
        if (s == null || t == null) {
            return 0;
        }
        int[][] nums = new int[s.length() + 1][t.length() + 1];

        for (int i = 0; i < s.length() + 1; i++) {
            nums[i][0] = 1;
        }

        for (int i = 1; i < s.length() + 1; i++) {
            for (int j = 1; j < t.length() + 1; j++) {
                nums[i][j] = nums[i - 1][j];
                if(s.charAt(i - 1) == t.charAt(j -1)) {
                    nums[i][j] += nums[i -1][j - 1];
                }
            }
        }
        return nums[s.length()][t.length()];
    }
}
时间: 2024-10-14 01:13:40

Distinct Subsequences Leetcode的相关文章

Distinct Subsequences leetcode 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 relat

Distinct Subsequences——Leetcode

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

Distinct Subsequences [leetcode] DP,空间复杂度O(T.size)

求S中和T相同的子串的个数. 递推公式: dp(i,j):S[0...i-1]和T[0...j-1]中不同子串的个数. dp(0,j) = 0 dp(i,0) = 1 一般情况下 dp(i,j) = dp(i-1,j-1) + dp(i-1,j)    S[i-1] == T[j-1],保留S[i-1]以及丢弃S[i-1] = dp(i-1,j)                       S[i-1] != T[j-1],丢弃S[i-1],只比较S[0...j-2]和T[0...i-1] 因为

[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

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

问题:给定两个字符串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 -day 15 Distinct Subsequences

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

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