题目:

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. 关于内存的5个函数(malloc,VirtualAlloc,GlobalAlloc,LocalAlloc,HeapAlloc)

    VirtualAlloc 该函数的功能是在调用进程的虚地址空间,预定或者提交一部分页,如果用于内存分配的话,并且分配类型未指定MEM_RESET,则系统将自动设置为0 一次分配 1PAGE 以上的 R ...

  2. KMeans聚类算法Hadoop实现

    Assistance.java  辅助类,功能详见注释 package KMeans; import org.apache.hadoop.conf.Configuration; import org. ...

  3. MDM基于IOS设备管控功能的所有命令介绍

    前面我们介绍了IOS上MDM几个简单的控制命令的发送和返回数据的解析处理,下面我们介绍一下MDM涉及到的命令的操作介绍: 一.Control Commands(控制类命令) 1.Device Lock ...

  4. Java多线程(一) 多线程的基本使用

    在总结JDBC数据库连接池的时候,发现Java多线程这块掌握得不是很好,因此回头看了下多线程的内容.做一下多线程模块的学习和总结,稳固一下多线程这块的基础.关于多线程的一些理论知识,这里不想啰嗦太多, ...

  5. 一些达成共识的JavaScript编码风格约定

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  6. Kubernetes Architecture

    reference:https://www.symantec.com/connect/blogs/google-kubernetes-analytical-evaluation

  7. 常用mysql命令大全

    常用的MySQL命令大全 一.连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再 ...

  8. DB2 SQL 递归实现多行合并

    最终效果 原始数据: 转换脚本: WITH post_a AS ( SELECT DISTINCT T.EMP_NO,S.CODE_ FROM inscndb.DTFMA000_EMP_POST T ...

  9. try、catch、finally的使用分析---与 return 相关

    看了一篇文章,讲解的是关于java中关于try.catch.finally中一些问题 下面看一个例子(例1),来讲解java里面中try.catch.finally的处理流程   1 2 3 4 5 ...

  10. WCF 动态生成 不用增加引用两种方式

    一.fromwork2.0低版本方式 1,打开vs的命令工具 输入:wsdl wcf地址 + /l:cs /out:文件名 上面红色部分替换掉就行,文件名,你想叫什么文件名都行. 2,回车,生成的文件 ...