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. js弹框处理

    # -*- coding:utf-8 -*- """ js弹框处理 """ from selenium import webdriver d ...

  2. C++之拷贝构造函数

    为什么要引入拷贝构造函数?(提出问题) 作用:创建一个对象的同时,使用一个已经存在的对象给另一个对象赋值 做比较:拷贝构造函数:对象被创建 +  用一个已经存在的对象 进行初始化 拷贝赋值函数:对象已 ...

  3. lvs,haproxy,keepalived,heartbeat

    lvs的是通过vrrp协议进行数据包转发的,提供的是4层的负载均衡.特点是效率高,只要你机器网卡抗的住就不是问题. haproxy可以提供4层或7层的数据转发服务,能做到7层的好处是可以根据服务所处的 ...

  4. Google图片搜索

    本博文的主要内容有 .Google图片搜索的介绍 .Google图片之普通搜索    .Google图片之高级搜索 1.Google图片搜索的介绍   Google的图片搜索,不仅通过关键字查找拥有特 ...

  5. shell获取 linux 系统的位数

    getconf LONG_BIT 直接返回  32  或者 64

  6. [RxJS] Marble diagrams in ASCII form

    There are many operators available, and in order to understand them we need to have a simple way of ...

  7. Qt解析XML文件(QXmlStreamReader)

    (2013-08-03 10:53:53) 转载▼       如何使用QXmlStreamReader来解析格式良好的XML,Qt的文档中指出,它是一种更快.更方便的Qt自己的SAX解析器(QXml ...

  8. Qt的皮肤设计(Style Sheet)

      Qt的皮肤设计,也可以说是对Qt应用程序的界面美化,Qt使用了一种类CSS的样式规则QSS. 一.Style Sheet的应用 1.直接在程序代码中设置样式,利用setStyleSheet()方法 ...

  9. 5 - SQL Server 2008 之 四则运算、比较运算、逻辑运算及字符连接运算

    四则运算如下: --加减乘除(+.-.*.\.%)取余运算 SELECT --加法运算 AS 加法结果2, --减法运算 -2.5 AS 减法结果1, 15.5+5.5 AS 减法结果2, --乘法运 ...

  10. swift 关于delegate

    Cocoa 开发中接口-委托 (protocol-delegate) 模式是一种常用的设计模式,它贯穿于整个 Cocoa 框架中,为代码之间的关系清理和解耦合做出了不可磨灭的贡献. 在 ARC 中,对 ...