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. BZOJ4445: [Scoi2015]小凸想跑步

    裸半平面交. 记得把P0P1表示的半平面加进去,否则点可能在多边形外. #include<bits/stdc++.h> #define N 100009 using namespace s ...

  2. PHP 数据库抽象层pdo

    PDO是PHP数据对象(PHP Data Object)的缩写. pdo就是一个"数据库访问抽象层",作用是统一各种数据库的访问接口,能够轻松地在不同数据库之间进行切换,使得数据库 ...

  3. Yoshua Bengio 2016年5月11日在Twitter Boston的演讲PPT

    Yoshua Bengio最新演讲:Attention 让深度学习取得巨大成功(46ppt) Yoshua Bengio,电脑科学家,毕业于麦吉尔大学,在MIT和AT&T贝尔实验室做过博士后研 ...

  4. UnityShader:HSV(色相,饱和度,亮度)转换

    http://blog.csdn.net/costfine/article/details/46930473 发现其实美术调整颜色的时候大部分都是调整的HSV,因为可以方便的分别调整色相(hue).饱 ...

  5. Error: [$rootScope:inprog] $digest already in progress

    我在 做一个 服务器分配成功以后需要更新 整个页面,我的思路是 更新成功以后,就手动的 触发一下 搜索按钮,但是在触发后,虽然成功刷新了页面,但是出现了一个 错误提示, Error: [$rootSc ...

  6. swiper 技巧

    全屏广告高度 ,加上属性. html, body { position: relative; height: 100%; } 停止自行播放 mySwiper.stopAutoplay(); 锁住状态, ...

  7. Ubuntu下如何安装YouCompleteMe插件

    Ubuntu安装YouCompleteMe插件 简介: 众所周知, Vim是一款linux下的文本编辑器, 其最基础的工作就是编辑文本,而不管该文本的内容是什么. 在Vim被程序员所使用后,其慢慢的被 ...

  8. js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果

    使用的TP3.2 JS字符串分割成字符串数组 var images='{$content.pictureurl} ' ;结构是这样 attachment/picture/uploadify/20141 ...

  9. JS验证URL正则

    方法一: function fIsUrL(sUrl) { var sRegex = '^((https|http|ftp|rtsp|mms)?://)' + '?(([0-9a-z_!~*\'().& ...

  10. Quartz-2D

    Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境.我们可以使用Quartz 2D API来实现许多功能,如基本路径的绘制.透明度.描影.绘制阴影.透明层.颜色管理.反锯齿 ...