30. Distinct Subsequences
Distinct Subsequences
OJ: https://oj.leetcode.com/problems/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.
思想:动态规划。 D[i][j] = D[i][j-1] + (T[i-1] == S[j-1] ? D[i-1][j-1] : 0);
// DP: D[i][j] = D[i][j-1] + (T[i-1] == S[j-1] ? D[i-1][j-1] : 0);
class Solution {
public:
int numDistinct(string S, string T) {
int m = T.length();
int n = S.length();
if(!m) return 1;
if(m > n) return 0;
vector<vector<int> > D(m+1, vector<int>(n+1));
for(int i = 1; i <= m; ++i) D[i][0] = 0;
for(int i = 0; i <= n; ++i) D[0][i] = 1;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
D[i+1][j+1] = D[i+1][j] + (T[i] == S[j] ? D[i][j] : 0);
return D[m][n];
}
};
改进后:空间复杂度 O(T.size()).
class Solution {
public:
int numDistinct(string S, string T) {
int m = T.length();
vector<int> num(m+1, 0); // num[i] is distinct numbers of T[1,...,i] in string S
num[0] = 1;
for(int i = 1; i <= S.length(); ++i)
for(int j = min(i, m); j >= 1; --j) // key to notice.
if(T[j-1] == S[i-1]) num[j] += num[j-1]; // num[j-1] 为上次字符串时,T[1,...,j-1] 的 distinct numbers。
return num[m];
}
};
30. Distinct Subsequences的更多相关文章
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- Leetcode Distinct Subsequences
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 T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- mysql 进程状态(转)
通过show processlist查看MySQL的进程状态,在State列上面的状态有如下这些: Analyzing线程对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ).c ...
- exp命令ORACLCE10G导出ORACLE11G的数据1455错误
异常:1455 解决: 命令:exp youruser/password@192.xxx.x.xx:1521/orcl owner=youruser file=c:\export.dmp trigge ...
- HTML5跨浏览器表单及HTML5表单的渐进增强
HTML5跨浏览器表单 http://net.tutsplus.com/tutorials/html-css-techniques/how-to-build-cross-browser-html5-f ...
- 【Andorid开发框架学习】之Mina开发之客户端开发
昨天我们讲到了Mina的基本知识点.如果还有不懂得同学可以看一下我昨天的博客.http://www.cnblogs.com/getherBlog/p/3934927.html今天我着重来讲一下基于Mi ...
- linq简介
语言集成查询(Language INtegrated Query,LINQ)是一项微软技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,可支持Visual Basic .N ...
- (C++) 基本面试题(整理)
1.new.delete.malloc.free关系 new/delete是C++的运算符.new 调用构造函数用于动态申请内存,delete调用对象的析构函数,用于释放内存. malloc与free ...
- [CSS]去除inline-block元素间距的几种方法
当我们使用inline-block 时,会出现空白间距问题. 但这些间距对我们的布局,或兼容性产生影响,我们需要去除它,该怎么办?下面简单介绍几种方法: 1.去掉html元素之间的空格,直接写在一行. ...
- Chrome每次打開都要打開123.sogou.com
剛開始還以為中毒了,又是殺毒又是掃描的,最後發覺,原來就是chrome的一個設置被改了. Chrome->設置->啟動時 : 選打开特定网页或一组网页->設置網頁 , 將其中的123 ...
- JAVA 根据用户输入数据求某年到某年有多少天
实例: import java.util.*; //求某年到某年有多少天 public class Test{ public static void main(String[] args){ Scan ...
- CentOS安装apache2(转载)
From:http://www.onepx.com/centos-apache-246.html 之前服务器 Apache 版本一直是 2.2.x,鉴于 Centos 更新软件的惰性,我看直到 201 ...