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.

这道题是求字符串T是字符串S的字串的所有可能性的数目(不存在就是0)。

刚开始算的的时候比较暴力,所以超时了。

public class Solution {
char[] word1;
char[] word2;
int result = 0;
public int numDistinct(String s, String t) {
int len1 = s.length(),len2 = t.length();
if( len1 < len2 )
return 0;
word1 = s.toCharArray();
word2 = t.toCharArray();
for( int i = 0;i<=len1-len2;i++){
if( word1[i] == word2[0])
helper(i+1,1);
}
return result; }
public void helper(int start1,int start2){ if( start2 == word2.length ){
result++;
return ;
}
for( int i = start1;i< word1.length ;i++){
if( word1[i] == word2[start2] )
helper(i+1,start2+1);
} }
}

所以用DP算法。

DP,化归为二维地图的走法问题。

r  a  b  b   i   t

1  0  0  0  0  0  0

r  1

a  1

b  1

b  1

b  1

i   1

t  1

如果当前字符相同,dp[i][j]结果等于用(dp[i-1][j-1])和(dp[i-1][j])求和

如果当前字符不同,dp[i][j] = dp[i-1][j]

public class Solution {

    public int numDistinct(String s, String t) {
int len1 = s.length(),len2 = t.length();
if( len1 < len2 )
return 0;
char[] word1 = s.toCharArray();
char[] word2 = t.toCharArray();
int[][] dp = new int[len1+1][len2+1]; for( int i = 0;i<=len1;i++){
for( int j = 0;j<=i && j<=len2;j++){
if( i == 0 && j != 0)
dp[i][j] = 0;
else if( j == 0)
dp[i][j] = 1;
else if( word1[i-1] == word2[j-1] )
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
else
dp[i][j] = dp[i-1][j]; }
} return dp[len1][len2];
} }

leetcode 115 Distinct Subsequences ----- java的更多相关文章

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

  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. 【一天一道LeetCode】#115. Distinct Subsequences

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

  7. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. 115. Distinct Subsequences

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

  9. 【leetcode】Distinct Subsequences(hard)

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

随机推荐

  1. Servlet下

    HTTP简介 HTTP是 hypertext transfer protocol(超文本传输协议)的简写,它是 TCP/IP 协议集中的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...

  2. 根据username查找user

    返回的是一个list<User>,不过验证密码的时候,要求返回是一个user对象,如果用uniqueresult,这个是过时的方法,如果用getResultList 会得到一个列表,get ...

  3. jQuery 中 children() 与 find() 用法的区别

    1.children() 与 find() 用法的区别 通过children获取的是该元素的下级元素,而通过find获取的是该元素的下级所有元素.

  4. vega prime 1.2 (视景仿真)

    Vega Prime 1.2 (视景仿真) MPI的视景仿真渲染工具Vega是世界上领先的应用于实时视景仿真.声音仿真和虚拟现实等领域的软件环境,它用来渲染战场仿真.娱乐.城市仿真.训练模拟器和计算可 ...

  5. Note_Master-Detail Application(iOS template)_02_YJYAppDelegate.m

    //YJYAppDelegate.m #import "YJYAppDelegate.h" #import "YJYMasterViewController.h" ...

  6. Cisco路由器的6种模式

    Cisco路由器的6种模式 -------------------------------------------------------------------------------------- ...

  7. iOS多线程之NSOperation,NSOperationQueue

    使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...

  8. UIkit框架介绍

    UIKit Framework The UIKit framework (UIKit.framework) provides crucial infrastructure for implementi ...

  9. PHP_string

    \n 换行 \r 回车 \t 制表符 \$ 美元符 \0 八进制数 \x 十六进制数 \\ 反斜杠字符

  10. Spring处理器

    Spring容器内部工作机制 Spring的AbstractApplicationContext是ApplicationContext抽象实现类,该抽象类的refresh()方法定义了Spring容器 ...