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.

解题思路:

dp问题,弄清递推公式即可,参考Distinct Subsequences@LeetCode ,JAVA实现如下:

	public int numDistinct(String s, String t) {
if (t.length() == 0)
return 1;
int[] dp = new int[t.length() + 1];
dp[0] = 1;
for (int i = 0; i < s.length(); i++) {
int last = 1;
for (int j = 0; j < t.length(); j++) {
if (dp[j] == 0)
break;
int temp = dp[j + 1];
if (s.charAt(i) == t.charAt(j))
dp[j + 1] += last;
last = temp;
}
}
return dp[t.length()];
}

Java for LeetCode 115 Distinct Subsequences【HARD】的更多相关文章

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

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

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  3. [leetcode]115. Distinct Subsequences 计算不同子序列个数

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  4. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  5. Leetcode#115 Distinct Subsequences

    原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...

  6. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 146 LRU Cache 【HARD】

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  8. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. Java for LeetCode 097 Interleaving String 【HARD】

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

随机推荐

  1. 【spring cloud】@EnableTransactionManagement注解的意义

    @EnableTransactionManagement注解的意义

  2. 网上常用免费webservice_查询(网络复制)

    MP3在线搜索服务 地址:http://www.wopos.com/webservice/song.asmx 介绍: 使用: getMusicList()方法搜索MP3/WMA等音乐文件 多功能条形码 ...

  3. TRIZ系列-创新原理-20-有效作用的连续性原理

    有效作用的连续性原理表述例如以下:1)连续实施动作不要中断,物体的全部部分应该一直处于满负荷工作状态.2)去除全部空暇的,中间的动作:3)用循环的动作取代"来来回回"的动作: 这个 ...

  4. OllyDbg 使用笔记 (一)

    OllyDbg 使用笔记 (一) 參考 书:<加密与解密> 视频:小甲鱼 解密系列 视频 ollydbg下载地址:http://tools.pediy.com/debuggers.htm ...

  5. HTML5 Canvas 绘制五角星

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  6. 安装htop教程及坑

    安装htop的坑:1.上官网http://hisham.hm/htop/releases/下载最新的包2.解压缩:tar -zxvf htop-2.0.2.tar.gz;3.进入目标文件夹: cd h ...

  7. Shell脚本之:数组

    bash支持一维数组,并且没有限定数组的大小,数组元素的下标由0开始编号. 定义数组 在Shell中,用括号来表示数组,数组元素用“空格”符号分割开.定义数组的一般形式为: array_name=(v ...

  8. 兔子--改动Android Studio的快捷键,改动成eclipse的快捷键

    仅仅须要2步 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill ...

  9. 2个YUV视频拼接技术

    http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...

  10. python之脚本参数optparse

    import optparse usage = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]" opter ...