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个字符的方案数。

或者这样理解,

假设现在只看前两个字符串的前i个和前j个,用长度为j的字符串去匹配长度为i的字符串有几种方法,

那就可以选择是否匹配最后一位,如果不匹配就去找dp[i-1][j],如果匹配就是dp[i-1][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()];
}
}

Distinct Subsequences Leetcode的更多相关文章

  1. Distinct Subsequences——Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  2. Distinct Subsequences leetcode java

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

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

  4. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  6. leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)

    https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...

  7. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  9. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

随机推荐

  1. sql 查询每月的销售金额

    sql 数据分月统计,表中只有每天的数据,现在要求求一年中每个月的统计数据(一条sql) SELECT   MONTH (  那个日期的字段  ),   SUM(  需要统计的字段, 比如销售额什么的 ...

  2. 闭包->类的实例数组排序

    简单的字符串数组排序就一句话 Arr.sort(function(s1,s2){return s1.localeCompare(s2));//升序 Arr.sort(function(s1,s2){r ...

  3. elasticsearch scroll api--jestclient invoke

    @Test public void testScroll(){ JestClientFactory factory = new JestClientFactory(); factory.setHttp ...

  4. Oracle11g的exp导出空表提示EXP-00011: 不存在

    刚lg问我11g无法导出空表,实验了下,果真如此. 原因:11g默认创建一个表时不分配segment,只有在插入数据时才会产生(当然也可以强制分配),以节省磁盘空间. 对于已经存在的空表解决办法: 就 ...

  5. lua 元表

    Set = {} Set.mt = {} function Set.new(t) local set = {} setmetatable(set, Set.mt) for _, l in ipairs ...

  6. ubuntu下安装TexLive和Texmaker

    也可以参考ubuntu14.04配置中文latex完美环境(texlive+texmaker+lyx) 设置中文字体的时候参考ubuntu 下安装 texlive 并设置 ctex 中文套装 1.首先 ...

  7. Which hashing algorithm is best for uniqueness and speed?

    http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness- ...

  8. ecshop 版权问题 Powered by ECShop

    将下代码 <div style="width:0px;height: 0px;overflow:hidden;">{foreach from=$lang.p_y ite ...

  9. yourphp 遇见问题及解决办法

    1.前台页面提交出现 __NOLAYOUT__ 解决把法: 在Public 文件下找到 success.html,error.html,exception.html 头部去掉就可以

  10. Description DisplayName Display的关系

    Description  DisplayName  Display的关系 ?