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. c++的默认构造函数 VS 深拷贝(值拷贝) 与 浅拷贝(位拷贝)

    C++默认为类生成了四个缺省函数: A(void); // 缺省的无参数构造函数 A(const A &a); // 缺省的拷贝构造函数 ~A(void); // 缺省的析构函数 A & ...

  2. 网络TCp数据的传输设计(黏包处理)

    //1.该片为引用别人的文章:http://www.cnblogs.com/alon/archive/2009/04/16/1437599.html 解决TCP网络传输"粘包"问题 ...

  3. Proxy(代理)-对象结构型模式

    1.意图 为其它对象提供一种代理以控制对这个对象的访问. 2.别名 Surrogate. 3.动机 通过Proxy可以达到对一个对象的访问控制. 4.适用性 远程代理  为一个对象在不同地址空间提供局 ...

  4. iOS打包ipa包

    创建证书之类这里就不说了,毕竟我也没有弄过. 我是团队开发,直接给我的p12文件,去开发者中心下载provision并且是distribute的就可以了. 1>.Xcode打包: 注意两个地方都 ...

  5. 1 Two Sum

    // Java public int[] twoSum(int[] nums, int target) { int[] answer = new int[2]; for (int i = 0; i & ...

  6. web页面全角&半角

    根据Unicode编码,全角空格为12288,半角空格为32 : 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248  全角-->半角函数  //半角转换 ...

  7. Itext Demo

    Tables and fonts /** * Example written by Bruno Lowagie in answer to the following question: * http: ...

  8. 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合

    转自:http://blog.csdn.net/zhao19861029/article/details/8473245 首先实现生成对应的JAVAbean的XML文件方法 /** * DMO4J写入 ...

  9. nodejs npm常用命令

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install m ...

  10. hdu 5876 (补图BFS) Sparse Graph

    题目:这里 题意: 相当于一开始给一个初始好了的无向完全图给你,然后给让你删除m条边,再给你一个点v,最后问你在剩下的图里从这个点v出发能到达所有边点的最小路径是多少? 一看是所有点的最小路径,一看就 ...