题目:

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.

链接: http://leetcode.com/problems/distinct-subsequences/

题解:

又一道读题意都很难的题目...又去discussion搬救兵了。看了以后才明白是给定pattern T, 问能在S的subsequence里有多少种包含T。很自然就又想到了dp的解法(当然是看了discussion后...)。 可以这样理解,有一个m x n的矩阵,一个机器人要从左上走到右下,只能向右或者向下走。当s.charAt(j - 1) == t.charAt(i - 1)时可以向下走, 每次向下走时count增加,向右走count不增加,,问到达右下角有多少种方法。  要注意初始化时,当 pattern = "" ,为空字符串时,第一行要初始为1。大家的理解是因为空字符串是任意字符串的subsequence。

Time Complexity - O(mn), Space Complexity - O(mn)。

public class Solution {
public int numDistinct(String s, String t) {
if(s == null || t == null)
return 0;
int[][] dp = new int[t.length() + 1][s.length() + 1]; for(int j = 0; j < dp[0].length; j++)
dp[0][j] = 1; for(int i = 1; i < dp.length; i++) {
for(int j = 1; j < dp[0].length; j++) {
if(s.charAt(j - 1) == t.charAt(i - 1))
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
else
dp[i][j] = dp[i][j - 1];
}
} return dp[t.length()][s.length()];
}
}

这种只依赖上一行的dp一般都可以用滚动数组来优化space complexity, 下面是自己写的比较丑的。用到了Arrays.fill以及 clone()。 有关clone(),doc上并没有说是deep copy还是shallow copy。 在这里应该用deepcopy,不过试了一下clone()居然能work。 有时间的话还是要好好研究。

Time Complexity - O(mn), Space Complexity - O(n)。

public class Solution {
public int numDistinct(String s, String t) {
if(s == null || t == null)
return 0; int[] last = new int[s.length() + 1];
int[] res = new int[s.length() + 1];
for(int j = 0; j < last.length; j++)
last[j] = 1; for(int i = 1; i < t.length() + 1; i++) {
for(int j = 1; j < res.length; j++) {
if(t.charAt(i - 1) == s.charAt(j - 1))
res[j] = res[j - 1] + last[j - 1];
else
res[j] = res[j - 1];
}
last = res.clone();
Arrays.fill(res, 0);
} return last[s.length()];
}
}

Reference:

https://leetcode.com/discuss/599/task-clarification

http://www.cnblogs.com/springfor/p/3896152.html

https://leetcode.com/discuss/19735/a-dp-solution-with-clarification-and-explanation

https://leetcode.com/discuss/2143/any-better-solution-that-takes-less-than-space-while-in-time

https://leetcode.com/discuss/7945/my-o-n-m-solution-for-your-reference

https://leetcode.com/discuss/26680/easy-to-understand-dp-in-java

115. Distinct Subsequences的更多相关文章

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

  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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 115. Distinct Subsequences (String; DP)

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

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

  6. 115. Distinct Subsequences *HARD* -- 字符串不连续匹配

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

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

  8. Leetcode 115 Distinct Subsequences 解题报告

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

  9. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

随机推荐

  1. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  2. What is a First Chance Exception?

    Refrences: http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx To be continued...

  3. CentOS 安装配置memcached (转)

    1.先下载memcached 和libevent. libevent 最新的稳定版: wget http://monkey.org/~provos/libevent-1.4.14b-stable.ta ...

  4. discuz x3插件开发傻瓜图文教程,用demo说话

    此demo功能是在模板footer部位插入一段javascript代码,这段代码可以是alert提示,也可以是加载广告等等. 第一步: 在config\config_global.php 文件里设置$ ...

  5. Oracle Lock 概述

    按锁的机制分类 排他锁( X ):如果事务T对对象A加上排他锁,则只允许T对A对象读取和修改,其他事务不能对A增加任何锁,直到T释放加载A上的排他锁 共享锁( S ):如果事务T对表A加上共享锁,则事 ...

  6. Oracle非默认监听的处理会遇到的问题以及处理方法

    第一种情况:只是修改默认端口 1.当前监听状态: C:\Windows\system32>lsnrctl status LSNRCTL for 64-bit Windows: Version 1 ...

  7. PS 颜色表大全-CMYK颜色表(2)

    CMYK颜色表 编号 C M Y K R G B 16进制值 1 0 100 100 45 139 0 22 8B0016 2 0 100 100 25 178 0 31 B2001F 3 0 100 ...

  8. Many To one 多对一

    一.创建实体类:多方存一方的对象.set/get 二.编写对象的xml文件 别忘记在confg.xml映射! 三.编写接口 四.方法测试

  9. Unity3D中Ragdoll的用法

    一.创建Ragdoll      见unity3d组件文档里的Ragdoll Wizard.由于unity3d中的Ragdoll设置的骨骼点名字与3DMAX里人体骨骼命名有些不一样,下图为Unity3 ...

  10. c++ 小片段

    void test_string() { string sen = "Connection will be closed if there is no " "read/w ...