[LeetCode OJ] Distinct Subsequences

时间:2022-09-28 09:08:55

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. &lbrack;LeetCode&rsqb; 115&period; Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  3. &lbrack;Leetcode&rsqb;&lbrack;JAVA&rsqb; 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&lpar;hard&rpar;

    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. &lbrack;leetcode&rsqb;115&period; 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&num;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. Ubuntu安装R及RStudio

    -------------------------------------------------------------- 自学记录,交流请发送邮件至gxz1984@gmail.com ------ ...

  2. 微信微博分享注意事项(sharesdk)

    0.(重要)如果接入多渠道可以考虑微博微信appid appkey等信息放到服务端,方便临时修改又可避免很多渠道时替换ShareSDK.xml文件出错. 但是cocos2dx-2.x版本使用代码配置a ...

  3. 【转】APUE学习1:迈出第一步,编译myls&period;c

    原文网址:http://blog.csdn.net/sddzycnqjn/article/details/7252444 注:以下写作风格均学习自潘云登前辈 /******************** ...

  4. UVW源码漫谈(番外篇)—— Emitter

    这两天天气凉了,苏州这边连续好几天都是淅淅沥沥的下着小雨,今天天气还稍微好点.前两天早上起来突然就感冒了,当天就用了一卷纸,好在年轻扛得住,第二天就跟没事人似的.在这里提醒大家一下,天气凉了,睡凉席的 ...

  5. Mysql基础安装,初视篇

    mysql 跟所有的数据库软件一样分为  服务端和客户端: 下载:在官网里面选择 download 社区版本,mysql,社区版本 安装: win环境下: 第一步:解压文件出来 第二步:在bin文件下 ...

  6. random模块&lpar;随机数&rpar;

    random.random() #0-1之间的随机数 random.randint(1,10) #1-10 包括10的随机数 --> int random.choice(list) #随机选取列 ...

  7. 微信小程序WebSocket报错:Error during WebSocket handshake&colon; Sent non-empty &&num;39&semi;Sec-WebSocket-Protocol&&num;39&semi; header but no response was received

    Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was ...

  8. leetcode — add-two-numbers

    import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * Source : https ...

  9. python中由于中文路径引起的os&period;path&period;isfile&lpar;imgpath&rpar; &equals;&equals; False问题

    昨天在用python脚本处理文件的时候,遇到了题述问题,明明文件时存在的,但是在用os.path.isfile(imgpath) == False进行判断的时候总是成立,在一开始以为是正反斜杠wind ...

  10. Maven编译时跳过Test

    在使用Maven编译项目时发现,可能在Test中写了一些有问题的代码,但是,由于写的代码比较多,所以不愿意去找具体的错误,反正Test中的代码不会影响项目的正常运行.于是想在编译时跳过对Test部分的 ...