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. struts启动报错Javassist library is missing

    很久不用struts2,最近在配置的时候,启动服务器报错 Caused by: java.lang.ExceptionInInitializerError at com.opensymphony.xw ...

  2. c#中string.trimstart() 和string.trimend() 的用法

    trim(),trimstart(),trimend()这样写是去掉空格,trimstart(a)是去掉字符串开始包含char[] a的字符,trimend同trimstart. 例:char[] a ...

  3. Microsoft SQL Server 管理 (常用管理及维护命令)

    --查询当前连接的实例名 select @@servername --察看任何数据库属性 sp_helpdb master --设置单用户模式,同时立即断开所有用户 alter database No ...

  4. 域名解析-delphi 源码

    unit Unit1; interface uses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  5. xp和win 2003远程桌面强制进入命令

    xp和win 2003远程桌面强制进入命令 注意:端口号也可以不写 如果是在win 2003里面进行踢人的话可以用命令: mstsc /console /v:172.25.100.27:3389 如果 ...

  6. rest例子

    http://www.xdemo.org/spring-restful/(可用) http://www.open-open.com/lib/view/open1389075258125.html(有例 ...

  7. jQuery.each的function中有哪些参数(可以大概理解function中的参数问题)

    1.没有参数 $("img").each(function(){ $(this).toggleClass("example"); }); 1 2 3 2.有一个 ...

  8. C#有关 字符串方法的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  9. ES6学习笔记之Promise

    入职百度EFE团队实习已经三周了,实习中接触到了生产环境的技术和开发流程,大开眼界,和自己在学校接小作坊式项目是很不一样的体验.其中一个很大的感触是,ES6早已不是“选修”的尝鲜技术,而是已经全面普及 ...

  10. 18款js和jquery文字特效代码分享

    18款js和jquery文字特效代码分享 jQCloud标签云插件_热门城市文字标签云代码 js 3d标签云特效关键词文字球状标签云代码 原生JS鼠标悬停文字球状放大显示效果代码 原生js文字动画圆形 ...