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以及其子串中distinct subsequences个数,那么T在S中的distinct subsequences个数也能通过它算出来。关键是如何找出转移函数。

使用题目中的例子,考虑简单的情况,S=rab,T=rab时,很明显number=1. 当S=rabb(多加一位)后,原有的number依然会有,因为rab还是能跟rab匹配。然而因为新加的字符与T的最后一个字符一样,这个时候T最后一个字符可以去匹配新加的字符,这个是在之前没有的情况,需要额外考虑。那么,多出来的distinct subsequences数就应该是排除掉T最后一个字符(匹配新字符去了)以及S中的新添加字符后的个数,即为S=rab,T=ra的情况。总的个数就是原来的number加上S=rab,T=ra情况下的个数。

如果S多加的一位不是T的最后一位字符,那么就不会有多出来的情况,这时候的distinct subsequences个数应该还是原来的个数。

创建一个二维数组,dp. dp[i][j]表示T.substring(0,i)和S.substring(0,j)的情况,0<=i<=T.length(), 0<=j<=S.length().

dp[i][j] = dp[i][j-1]  //原有情况

    +   dp[i-1][j-1]  //如果T.charAt(i-1)==S.charAt(j-1)

起始情况dp[0][j]=1, 因为T为空字符串时匹配个数始终为1.

dp[i][j]=0如果j<i因为当T长度大于S肯定没有这样的subsequences.

代码如下:

     public int numDistinct(String S, String T) {
if(T.length()>S.length())
return 0;
int[][] dp = new int[T.length()+1][S.length()+1];
for(int i=0;i<=T.length();i++) {
for(int j=i;j<=S.length();j++) {
if(i==0)
dp[i][j]=1;
else
dp[i][j] = dp[i][j-1]+(T.charAt(i-1)==S.charAt(j-1)?dp[i-1][j-1]:0);
}
}
return dp[T.length()][S.length()];
}

因为每次循环只需要拿到上一次的dp值,所以可以对空间复杂度进一步优化:

     public int numDistinct(String S, String T) {
int m = T.length();
int n = S.length();
if(m>n)
return 0;
int[] dp = new int[m+1];
dp[0] = 1;
for(int j=1;j<=n;j++)
{
for(int i=m;i>0;i--)
{
dp[i] += (T.charAt(i-1)==S.charAt(j-1))?dp[i-1]:0;
}
}
return dp[m];
}

[Leetcode][JAVA] Distinct Subsequences的更多相关文章

  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 ----- java

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

  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】Distinct Subsequences(hard)

    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

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

  6. [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 ...

  7. Leetcode 115 Distinct Subsequences 解题报告

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

  8. Leetcode#115 Distinct Subsequences

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

  9. Distinct Subsequences leetcode java

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

随机推荐

  1. ajax的表单提交,与传送数据

    ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: ...

  2. JS之函数表达式

    度过一个愉快短暂的周末,又可以开始学习了!我爱学习,学习使人进步.今天学习函数表达式,着重学习下闭包函数. 函数表达式 可以在定义的函数声明之前调用它,但是不能在定义函数表达式之前调用它 /** * ...

  3. spring filter拦截器

    实现的功能:判断用户是否已登录,未登录用户禁止访问任何页面或action,自动跳转到登录页面.比较好的做法是不管什么人都不能直接访问jsp页面,要访问就通过action,这样就变成了一个实实在在的权限 ...

  4. C#中 字符串转换为计算公式

    //方法一 利用DataTable中的Compute方法 例如:1*2-(4/1)+2*4=6 string formulate = string.Format("{0}*{1} - {2} ...

  5. 记一次PHP7+opcache+zmq出现SIGSEGV 问题的查找(一次不成功的bug查找)

    Title:  记一次PHP7+opcache+zmq出现SEGSEGV问题的查找(一次不成功的bug查找) bug来历自述:线上代码PHP环境是5.2,为了提升性能(逼格),于是升级为PHP7并使用 ...

  6. 使用angularJS遇见的一些问题的解决方案

    1. angularJS的$http.post请求,SpringMVC后台接收不到参数值的解决方案 问题一般为:400 Required String parameter 'rPassword' is ...

  7. Ubuntu 15.10 x64 安装 Android SDK

    操作系统:Ubuntu 15.10 x64 目标:安装 Android SDK 本文最后更新时间:2015-11-3 安装32位库文件 2013年9月的iPhone 5s是第一款64位手机,而Andr ...

  8. [Note] changing building platform from vs 2013 to vs community 2015

    The error turned out as "undefined linkage"(The same as you haven't use some function that ...

  9. whoami与who am i

    whoami显示的是当前“操作用户”的用户名,而who am i显示的是“登录用户”的用户名. 若你使用root用户登录,中间su abc切换,whoami的结果是abc,who am i 的结果是r ...

  10. Android(Xamarin)之旅(五)

    2016年1月23日,北京迎来了很痛苦的一天,冻死宝宝了,一天都没有出我自己的小黑屋,在这屋子里自娱自乐.不知道你们呢 对于android的四大基本组件(Activity.Service.Broadc ...