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

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Matlab与DSP联合开发

    1.关于DSP开发环境 刚开始接触TI CCS的时候,用的是CCS2.2,当时CCS2.2又分成4个系列安装包 1.CCS6000 2.CCS5000 3.CCS2000 4.OMAP 都可以单独安装 ...

  3. HDOJ1518Square 深搜

    Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. udev:renamed network interface eth0 to eth1

    删除/etc/udev/rules.d/70-persistent-net.rules这个文件,重启

  5. Hadoop Hive概念学习系列之什么是Hive?(一)

    参考  <Hadoop大数据分析与挖掘实战>的在线电子书阅读                   http://yuedu.baidu.com/ebook/d128cf8e33687e21 ...

  6. 使用php将数组转为XML

    <?php class Array_to_Xml { private $version = '1.0'; private $encoding = 'UTF-8'; private $root = ...

  7. Python - 元组(tuple) 详解 及 代码

    元组(tuple) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290967 元组是存放任意元素集合,不能修 ...

  8. Sencha Touch 的基础知识

    基础知识 一.要注意书写,一个字母就会让你代码跑不起来Ext.setup({ icon:'icon.png', glossOnIcon:false, tabletStartupScreen:'talb ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录 我们之前做了验证码,登录界面,却没有登录实际的代码,我们这次先把用户登录先 ...

  10. QT 读写sqllite数据库

    QT 读写sqllite数据库 分类: 技术资料2014-04-10 10:39 84人阅读 评论(0) 收藏 举报 #include <QtGui/QApplication> #incl ...