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.

方法一:用回溯法实现,时间复杂度很高,空间复杂度低,对于小数据可以通过,对大数据会出现Time Limit Exceeded

 int num=;
void countnum(string S, string T) {
if(T.size()==)
{
num++;
return;
} for(int i=; i<S.size(); i++)
{
if(S[i]==T[])
{
string s2 = S.substr(i+);
string t2 = T.substr();
countnum(s2, t2);
} }
return;
} class Solution {
public:
int numDistinct(string S, string T) {
countnum(S, T);
return num;
}
};

方法二:用动态规划(DP)实现,需要的空间复杂度为O(N*M),对于大数据也可以很快处理。

 class Solution {
public:
int numDistinct(string S, string T) {
vector<vector<int> > num(S.size()+,vector<int>(T.size()+,)); //num[i][j]表示T中的前j个字符构成的子字符串在S中的前i个字符中出现的次数,num[i][j]满足:
S = " "+ S; //(1)若S[i]=T[j],则num[i][j] = num[i-1][j]+num[i-1][j-1];
T = " "+ T; //(2)若S[i]!=T[j],则num[i][j] = num[i-1][j];
num[][]=; //(3)若j>i,则num[i][j]=0。
for(int i=; i<S.size(); i++)
for(int j=; j<T.size(); j++)
{
if(j>i)
{
num[i][j]=;
break;
}
if(S[i]==T[j])
num[i][j] = num[i-][j] + num[i-][j-];
else
num[i][j] = num[i-][j];
}
return num[S.size()-][T.size()-]; }
};

[LeetCode OJ] Distinct Subsequences的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [Leetcode][JAVA] Distinct Subsequences

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

  4. 【leetcode】Distinct Subsequences(hard)

    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 ----- java

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

  6. [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 ...

  7. Leetcode 115 Distinct Subsequences 解题报告

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

  8. Leetcode#115 Distinct Subsequences

    原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...

  9. 【LeetCode OJ】Distinct Subsequences

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

随机推荐

  1. 深入浅出Node.js (附录D) - 搭建局域NPM仓库

    D.1 NPM仓库的安装 D.1.1 安装Erlang和CouchDB D.1.2 搭建NPM仓库 D.2 高阶应用 D.2.1 镜像仓库 D.2.2 私有模块应用 D.2.3 纯私有仓库 D.3 总 ...

  2. u-boot中分区和内核MTD分区关系

    一.u-boot中环境变量与uImage中MTD的分区关系 分区只是内核的概念,就是说A-B地址放内核,C-D地址放文件系统,(也就是规定哪个地址区间放内核或者文件系统)等等. 一般我们只需要分3-4 ...

  3. MYSQL删除以数字开头的字段

    例子: // 删除以0开头的字段 DELETE FROM `week_energy_copy` WHERE openid like '0%'; // 删除以数字开头的字段 DELETE FROM `w ...

  4. unity3d AI's sight

    just finished -----by wolf96

  5. HDOJ/HDU 1039 Easier Done Than Said?(字符串处理~)

    Problem Description Password security is a tricky thing. Users prefer simple passwords that are easy ...

  6. int 和 long的区别

    数据模型决定了C语言中基本数据类型与数据宽度的对应关系.我们常用的32位模型称为ILP32,而64位模型有三种:LP64.LLP64和ILP64.在64位模型中,指针一定是64位的,但是int和lon ...

  7. 短随机唯一id生成参考

    <?php function getRandOnlyId() { //新时间截定义,基于世界未日2012-12-21的时间戳. $endtime=1356019200;//2012-12-21时 ...

  8. disconf实践(一)

    公司目前的应用基本采用分布式部署,通过F5进行集群管理.分布式应用带来的好处是,随着流量的增加,可以快速扩展应用节点,分摊压力.分布式也会带来一定的挑战,譬如配置文件管理.如果某个配置要修改,那么所有 ...

  9. for循环++i效率

    偶然用到for循环 注意查看了一下 ++i的速度更快 这是PHP语言独有的 其他语言不会有这种情况

  10. win8 64位 mysql安装 Configuration file my.ini error code -1

    问题如题,解决方法: 1.由于目录中纯在中文,所以导致代码错误.故而把目录设置成全英文的. 2.下载64位mysql安装,安装32位mysql也会出现此问题.