[LeetCode OJ] 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
.
方法一:用回溯法实现,时间复杂度很高,空间复杂度低,对于小数据可以通过,对大数据会出现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的更多相关文章
- 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 ...
- [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 ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【leetcode】Distinct Subsequences(hard)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 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 ...
- [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 ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- Leetcode#115 Distinct Subsequences
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- Mysql慢日志查询
MYSQL慢查询配置: 查看是否启用慢日志查询: show VARIABLES like '%quer%'; 没有配置的情况下慢查询是关闭的:
- Linux学习笔记24——进程管道
一 管道的作用 通常把一个进程的输出通过管道连接到另一个进程的输入. 二 popen和pclose函数 #include <stdio.h> FILE *popen(const char ...
- Sysrq 诊断系统故障 与 gdb 调试core dump
1. 典型应用场景如: 1)系统进入了挂死状态(如调度出现异常.或系统负荷过重),但仍能响应中断,此时可以通过Sysrq魔术键(c)手工触发panic,结合kdump,就能收集到vmcore信息 ...
- 5 kafka整合storm
本博文的主要内容有 .kafka整合storm .storm-kafka工程 .storm + kafka的具体应用场景有哪些? 要想kafka整合storm,则必须要把这个storm-kafk ...
- CentOS 6.5 下安装 Redis 2.8.7(转)
转自:http://www.cnblogs.com/haoxinyue/p/3620648.html CentOS 6.5 下安装 Redis 2.8.7 wget http://download.r ...
- Drawing Lines - SGU 135(简单递推)
求N条直线最多能把一个平面分成几部分. 代码如下: ========================================================================== ...
- mysql 导出,导入数据
导出 加-d代表只导出表结构 命令行下具体用法如下: mysqldump -u用户名 -p密码 -d 數據库名 表名 脚本名; 1.导出数据库为dbname的表结构(其中用户名为root, ...
- poj3624背包问题(一维数组)
Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...
- baidu
[百度凤巢业务实习] 查看打赏情况 打赏100shb 打赏1000shb 打赏10000shb 只看此用户 大字体 楼主 百度凤巢业务--百度最核心的业务做数据挖掘,机器学习,广告算法等,主要 ...
- hadoop单线程实现server多socket连接读取数据原理分析
一.问题引出. Hadoop 的Server 采用了Java 的NIO,这样的话就仅需要为每一个socket 连接建立一个线程,读取socket 上的数据.在Server 中,只需要一个线程,就可以a ...