115. Distinct Subsequences
题目:
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的更多相关文章
- [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 ...
- 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 ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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 ...
- [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 ...
- 115. Distinct Subsequences *HARD* -- 字符串不连续匹配
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 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 ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
随机推荐
- spring aop配置及用例说明(3)
欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3476336.html 1.这里说一下aop的@Around标签,它提供了在方法开始和结束,都能添加用户业务逻 ...
- Tomcat虚拟目录的设置
在学习JSP/Servlet的过程中,配置Tomcat的虚拟目录可能是我们遇到的第一个比较麻烦的问题,说是麻烦是针对我们初学者而言,对于高手那都不是问题.反正我是弄了一天才配置好,发现网上给出的很多配 ...
- qml实现窗口拖动
在去掉窗口标题栏后窗口会失去鼠标拖动效果,所以需要自己添加拖动效果. 实现代码: ApplicationWindow { id: mainWindow visible: true ...
- 译文:javascript function中的this
个人理解+google翻译.如有错误,请留言指正.原文来自MDN: this 简介 Javascript中一个函数的this关键字的行为相对其它语言有些不同.在严格模式和非严格模式间也有区别. 在大多 ...
- 连接Oracle数据库的OracleHelper.cs
using System; using System.Configuration; using System.Data; using System.Data.OracleClient; using S ...
- TreeView递归绑定数据的两种方法
#region 绑定TreeView /// <summary> /// 绑定TreeView(利用TreeNode) /// </summary> /// <param ...
- NPOI Excel导入 导出
添加引用 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Gene ...
- [原创] linux课堂-学习笔记-目录及概况
本学习笔记基于:网易云课堂-linux课堂 课时1Centos 6.4安装讲解46:14 课时2Centos 6.4桌面环境介绍与网络连接04:30 课时3 Linux目录结构介绍及内核与shell分 ...
- Sublime Text 前端插件推荐
html/CSS快速编辑 --- Emment HTML CSS JS 美化插件 --- HTML/CSS/JS Prettyfy MarkDown 预览 --- MarkDown Preview J ...
- zhuan:ubuntu下安装Apache2+php+Mysql
from: http://www.cnblogs.com/lynch_world/archive/2012/01/06/2314717.html ubuntu下安装Apache+PHP+Mysql 转 ...