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. LDAP客户端

    LDAP客户端通过与服务端关联起来,就可以使用服务端的系统账号登录系统,通过useradd 添加用户是在ldap里是没有显示的,ldap添加用户,在/etc/passwd里也是没有显示的,ldap添加 ...

  2. python第二十天-----Django补充

    学习python已经20天了啊,从一个不萌萌哒的战5渣升级到了一个萌萌哒的战5渣 1.分页(这是一个很通用的模块,不论在任何框架里都可以使用哦) class Page(object): def __i ...

  3. python实验一:画图

    题目:画图,学用rectangle画方形. rectangle(int left, int top, int right, int bottom) 参数说明:(left ,top )为矩形的左上坐标, ...

  4. python autopep8

    安装 使用pip install autopep8或easy_install 都可以. 使用 autopep8 -i -a 要检查的py文件路径 更多参数使用可以参考:https://github.c ...

  5. [刘阳Java]_MyBatis_映射文件的resultMap标签入门_第4讲

    <resultMap>:用于解决实体类中属性和表字段名不相同的问题 id:表示当前<resultMap>标签的唯一标识 result:定义表字段和实体类属性的对应关系 prop ...

  6. java javacv调用摄像头并拍照

    调用摄像头并拍张照片,我一开始用的java的jmf媒体框架,但这个有很多的局限性不好使并且很有麻烦,兜了一圈发现javacv东西,研究之后这东西简单,方便:废话不多说了来重点. javacv官网:点击 ...

  7. vps云服务器建站后绑定域名的方法?

    有很多的新手站长们,都不知道vps建站后该如何绑定自己的域名,这里就Windows系统的VPS主机利用iis绑定网站域名的方法,简要介绍一下. 通常情况下,我们在使用IIS建站的时候,都会有一步提示, ...

  8. HTML5中canvas大小调整

    今天用到canvas元素,发现它的大小不是像普通dom元素一样,直接设置css样式可以改变的,它会由自己原本的大小伸缩. 例如, <canvas id='canvas'></canv ...

  9. 无法删除服务器 'old_server_name',因为该服务器用作复制过程中的发布服务器。 (Microsoft SQL Server,错误: 20582)

    无法删除服务器 'old_server_name',因为该服务器用作复制过程中的发布服务器. (Microsoft SQL Server,错误: 20582) 2013-01-05 15:02 478 ...

  10. Hadoop2.6.0的事件分类与实现

    前言 说实在的,在阅读Hadoop YARN的源码之前,我对于java枚举的使用相形见绌.YARN中实现的事件在可读性.可维护性.可扩展性方面的工作都值得借鉴. 概念 在具体分析源码之前,我们先看看Y ...