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. Debian环境下vi设置

    下面给出一个vi编辑器的配置文件,可以放到用户目录的.vimrc文件中: "========================================================= ...

  2. 【jmeter】JMeter中返回Json数据的处理方法

    Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...

  3. 剑指offer系列61---数组中的逆序对

    [题目]在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. * [思路]运用归并排序的思想. * 首先将数组分成两个子数组,统 ...

  4. 【转】java int与integer的区别

    java int与integer的区别 int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1 ...

  5. Python之Fabric模块

    Fabric是基于Python实现的SSH命令行工具,简化了SSH的应用程序部署及系统管理任务,它提供了系统基础的操作组件,可以实现本地或远程shell命令,包括:命令执行.文件上传.下载及完整执行日 ...

  6. Thinkpad X240修改bios引导方式

    来源:http://blog.csdn.net/jsship/article/details/19121149 修改笔记本的BIOS设置!这是非常重要的步骤之一.否则,你的U盘不能引导手提电脑进入PE ...

  7. C语言每日一题之No.9

    再做决定之前,我还是做好自己该做的.我不希望几年后会悔恨自己为什么在最该努力的时候不愿意吃苦.尊敬的女王陛下,请接题: 一.题目:有已按升序排好顺序的字符串a,编写程序将字符串s中的每个字符按升序的规 ...

  8. JavaScript中Call()以及Apply()的应用

    apply()和call()的真正用武之地是能够扩充函数赖以运行的作用域 三点说明: 1.每个函数都包含两个非继承而来的方法:apply()和call(). 2.他们的用途相同,都是在特定的作用域中调 ...

  9. hbase读写流程

    一. Hbase读流程 META表记录着表的原信息,根据rowkey查询META表,获取所在region信息 客户端去相应的regionServer查询数据,先查询memStore(memstore是 ...

  10. Hibernate第一个程序

    1. 下载资源:www.hibernate.org 2. 资源介绍hibernate-release-4.3.10.Final a) Documentation  相关文档 b) Lib 相关jar包 ...