题目:

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. 使用python读写windows剪切板

    import win32clipboard as w import win32con base_addr = 0x8e00000 buffer_len = 0x123 def getText(): w ...

  2. 认识HTML5

    引言,认识两个标准制定的组织 在讲什么是Html5之前得先了解两个组织:WHATWG :网页超文本技术工作小组(英语:Web Hypertext Application Technology Work ...

  3. eclipse 安装egit 成功后Team中没有显示

    主要是版本不太对. 在http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of_EGit.3F 中找到对应的版本,设置就O ...

  4. jsp日期控件My97DatePicker的使用

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 2.将My97DatePicker包放在项目WebContent目录下 3.在页面 ...

  5. 十八、mysql 内存优化 之 myisam

    .key_buffer 索引块大小 set global hot_cache.key_buffer_size = ; //设置大小 show variables like 'key_buffer_si ...

  6. 拉链法解决Hash节点冲突问题

    <?php /* * hash::拉链法解决hash节点存储冲突问题 * ::2014-07-02 * ::Small_Kind */ class small_hash { private $s ...

  7. C#基础及记忆概念

    在C#中,你给一个方法传输值类型参数时,实际上是使用的这个参数的一个副本,就是将原来的变量复制一份,然后传给一个方法,让其进行操作.所以在方法内部对参数的修改等不会对原来的参数造成影响(这个其实就是值 ...

  8. Linux操作系统

    Linux操作系统 linux源码分析(三)-start_kernel 2016-10-26 11:01 by 轩脉刃, 146 阅读, 收藏, 编辑 前置:这里使用的linux版本是4.8,x86体 ...

  9. Oracle和Redhat下载地址

    Oracle RedHat 用户名:zhangwei900808@126.com 密码:@XxxxxXxxxxxx 有网友想要的,请在留言板给我留言,我会把用户名和密码发给你!

  10. 使用Sqlserver事务发布实现数据同步

    事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案,所以有机会再次研究一下它以及快照等,发现还是有很多不错的功能和改进 的.这里以sqlserver2008的事务发布功能为例,对发 ...