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.

题目大意:给两个字符串S,T,问从S中可以有多少不同的子序列是T,子序列只能删源字符串元素。

解题思路:

动态规划,这个题要复杂一些,首先来看怎么定义状态是最关键的,题目要求是从S到T,如果S中只有一个合法的T,结果应该是1。

dp[i][j]表示字符串S[0...i]到T[0...j]具有多少种变化形式,首先可以确定的是,dp[i][0]=1,意思表示从S到空字符串变换形式只有一种,就是删除S中所有的字符串。

除此之外,如果S[i]!=T[j],dp[i][j]=dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。

如果S[i]==T[j],dp[i][j]=dp[i-1][j-1]+dp[i-1][j],意思是如果当前字符不等,那么可以抛弃当前这个字符,也可以要这个字符,dp[i-1][j-1]就表示从字符串S[0...i-1]到T[0...j-1]的变化次数,dp[i-1][j]就表示从字符串S[0...i-1]到T[0...j]的变化次数,这两个加起来就表示要和不要这个字符一共有多少种变化形式。

    public int numDistinct(String s, String t) {
if(s==null||t==null){
return 0;
}
int[][] dp = new int[s.length()+1][t.length()+1];
for(int i=0;i<=s.length();i++)dp[i][0]=1;
for(int i=1;i<=s.length();i++){
for(int j=1;j<=t.length();j++){
if(s.charAt(i-1)==t.charAt(j-1)){
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}else{
dp[i][j]=dp[i-1][j];
}
}
}
return dp[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. PHP 内存的分布问题

    php运行,内存分为5个区域, 1.基本数据类型--->栈区 2.符合数据类型-->堆区 对象实例在堆区,对象名字在栈区(指向此对象实例的变量)

  2. mvc5 + ef6 + autofac搭建项目(四)

    在列表页面,点击新增,弹出窗口实现视屏上传,这里存在一个问题,就是大文件上传的问题,iis出于安全问题,有限制,当然这不是大问题,解决也很容易: 见截图: 请忽略视屏文件,看得懂的请装作不懂. 源码 ...

  3. html5与js关于input[type='text']文本框value改变触发事件一些属性的区别oninput,onpropertychange,onchange和文本框的value点击全选状态onclick="select();"。做购物车页面时会要用到。

    关于input[type='text']文本框value改变触发事件一些属性的区别oninput,onpropertychange,onchange和文本框的点击全选状态onclick="s ...

  4. EDM推送

    一.需求描述:        日前,做了一个发送客户账单的功能,邮件模板采用自定义,生成vm文件,保存至redis,    采用jodd-mail发送邮件,查询用户账单数据,账单明细,缓存加载模板并渲 ...

  5. codevs 4511 信息传递(NOIP2015 day1 T2)

    4511 信息传递 NOIP2015 day1 T2 时间限制: 1 s 空间限制: 128000 KB 传送门 题目描述 Description 有个同学(编号为 1 到)正在玩一个信息传递的游戏. ...

  6. [转]python yield

    任何使用yield的函数都称之为生成器,如: def count(n): while n > 0: yield n   #生成值:n n -= 1 另外一种说法:生成器就是一个返回迭代器的函数, ...

  7. sass学习--什么是sass

    1.预备知识--什么是 CSS 预处理器 CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些编程的特性,将 CSS 作为目标生成文件,然后开发者就只要使用这 ...

  8. C# Winform开发框架模块图(平台核心模块+示例模块)

    企业版V4.0 - 模块图   企业版V4.0 - 项目解决方案   Client/Server构架,有两个解决方案:     客户端解决方案说明:     服务端解决方案说明: C/S系统开发框架- ...

  9. 了解SOA是什么!

    面向服务架构 面向服务的体系结构(service-oriented architecture,SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来 ...

  10. python 读入

    2 3 4 f=open('message1.txt','r') #这个message1.txt文件应该和这个.py的文件放在同一文件夹下 或者是把路径写全 例: f=open('c:/message ...