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. Deep Learning In NLP 神经网络与词向量

    0. 词向量是什么 自然语言理解的问题要转化为机器学习的问题,第一步肯定是要找一种方法把这些符号数学化. NLP 中最直观,也是到目前为止最常用的词表示方法是 One-hot Representati ...

  2. C#常用实例

    1 時間 1.1 顯示在走的時間 控件:TextBox為顯示日期時間,命名為txtDateTimer Timer為時鐘,命名為time private void dtDateTimer_Tick(ob ...

  3. 企业需要k2来解放孤岛危机

    当我谈孤岛危机时,我谈些什么?你以为我要说的是一款风靡的游戏?那恐怕要让你失望了,今天要谈的是“企业管理体系孤岛”,但更多人甚至都没意识到这是危机. 下面的场景,也许你会觉得似曾相识. 场景一 某制鞋 ...

  4. iOS - CADisplayLink与NSTimer

    一.CADisplayLink简介 CADisplayLink 是一个定时器对象可以让你的应用以与显示器的刷新界面相同的频率进行绘图. 应用如果想要创建 display link ,需要提供一个目标对 ...

  5. ubuntu 添加源

    edit file :  /etc/apt/sources.list add: deb http://mirrors.163.com/ubuntu/ intrepid main restricted ...

  6. ODI 12.1.3创建standalone代理

    首先要安装ODI. ODI安装 如果没有安装WLS,则可以选择独立安装,如下图.

  7. hql抓取要注意的点

    fetchtype是lazy,那就用到了在通过缓存中的关联去取,用不到不取:lazy遇到joinfetch就失去意义,但是由于hql语句是自己编写的,可以控制加不加fetch 所以如果主力是hql语句 ...

  8. Javascript基础--成员函数(六)

    成员函数:也叫方法 1.常用方法 比如:我们希望对象不但有属性,还希望他有行为.(行为在程序中要靠函数来体现)(1) 添加speak函数,输出我是一个好人 (2) 添加jisuan函数,可以计算从1+ ...

  9. poj3126 筛素数+bfs

    //Accepted 212 KB 16 ms //筛素数+bfs #include <cstdio> #include <cstring> #include <iost ...

  10. 一点点webservice的小知识

    怕自己忘了记录下来好了 在web.config中要配置自己要调用的webservice的地址 在自己controller中获取web.config中配置的地址 SystemManager.Config ...