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的更多相关文章

  1. Leetcode 115 Distinct Subsequences 解题报告

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

  2. [LeetCode] Distinct Subsequences 不同的子序列

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

  3. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  4. Leetcode Distinct Subsequences

    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 T in S. A subsequen ...

  6. [Leetcode][JAVA] Distinct Subsequences

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

  7. Distinct Subsequences Leetcode

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

  8. 【leetcode】Distinct Subsequences(hard)

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

  9. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

随机推荐

  1. sql2008 将行转为字符串, 将字符串转为行 互转

    --将行转为字符串 select stuff((select top 20 ','+ QQ from dl_QQ where uiid=1 order by tim desc for xml path ...

  2. MVC4 WebAPI(一)

    http://www.cnblogs.com/wk1234/archive/2012/04/28/2468491.html 不管是因为什么原因,结果是在新出的MVC中,增加了WebAPI,用于提供RE ...

  3. Maven的几个核心概念

    POM (Project Object Model) 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它 ...

  4. nginx的https配置

    测试自签名的ssl证书 首先执行如下命令生成一个key openssl genrsa -des3 - 然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次reload ...

  5. C++设计新思维的traits和policy

    http://blog.csdn.net/zhoudaxia/article/details/4486487 这篇博客讲得挺清楚的,本来想自己写写看总结下的,不过看了下这个文章已经写得很清楚了,倒没有 ...

  6. 验证视图状态 MAC 失败

    起因: 最近在做一个项目需要用到生成多个Html页,采用一下方法动态生成. WebRequest request = WebRequest.Create(pageurl); WebResponse r ...

  7. 一款灵活好用的日历控件Kalendae

    Kalendae是一款纯js不依赖任何js库的日历控件,可以轻松实现显示月份数量,当前选中多个日期,并可以按照周等你想要的格式去定制选中项. 下载地址:GitHub/Kalendae 第一步:Kale ...

  8. extern 修饰符

    extern(C# 参考) extern 修饰符用于声明在外部实现的方法. extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 特性一起使用.在这种情况 ...

  9. iphone dev 入门实例6:How To Use UIScrollView to Scroll and Zoom and Page

    http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content Getting Starte ...

  10. Form_Form Builder Export导出为Excel(案例)

    2014-01-09 Created By BaoXinjian