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. Volley简单封装

    public interface IRequest { /** * 获取头部信息 * * @return */ public Map<String, String> getHeaderMa ...

  2. ssh批量互信脚本

    #!/bin/sh#date:2016-05-17#wrinte:lxh cat ./iplist.txt |grep -v "^$" >iplist.tmpiplist=. ...

  3. Android 手机自动化测试工具有哪几种?

    1.Monkey是Android SDK自带的测试工具,在测试过程中会向系统发送伪随机的用户事件流,如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试,也有日志输出.实际上该工 ...

  4. draw: Could not use program error=0x505

    原因:Android的模拟器在ADT中调试运行AVD时速度太慢.也就是说创建的虚拟手机配置太好,电脑带不动. 解决办法:从新创建虚拟手机,是手机配置低一点,具体创建方法如下: 开始先点运行,也就是下图 ...

  5. ORA-12514 TNS:listener does not currently know of service requested in connect descriptor

    this issue and the fix was to make sure in tnsnames.ora the SERVICE_NAME is a valid service name in ...

  6. checkbox全选-取消-再全选没有显示问题

    源码: <input type="checkbox" id="cleckAll" />全选 <div class="list&quo ...

  7. a 标签提交前验证

    最近在做验证的时候遇到了submit()与onsubmit()事件冲突的问题,本来想在a标签中添加submit()进行表单的提交,然后在 form中添加onsubmit事件触发验证方法.结果行不通,最 ...

  8. 关于安装sql2012出现的netfx3功能问题

    这个问题需要下载framework3.5即可继续正常安装,所以说低版本的framework也是有必要安装的

  9. 用Action的属性接受参数

    版本, @Override是Java5的元数据,自动加上去的一个标志,告诉你说下面这个方法是从父类/接口 继承过来的,需要你重写一次,这样就可以方便你阅读,也不怕会忘记@Override是伪代码,表示 ...

  10. mysql 锁优化

    一.myisam存储引擎锁优化 1.合理理由读写优先级MyISAM 的表锁,写互相阻塞的表锁,默认系统是写优先,可改为读有先:low_priority_updates=1如果我们的系统是一个以读为主, ...