[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 ...
随机推荐
- page-object使用(2)---elements
elements就是html元素下所有的标签.用page-object你可以找到并定位html页面下绝大多数的元素,这个文章列出了可定位的这些元素,生成的方法,和依据什么关键字来找到这些元素. BUT ...
- Bayer RGB和RGB Raw
Bayer RGB和RGB Raw 对于SENSOR来说,Bayer RGB和RGB Raw两者的图象结构都是BG/GR的(Bayer pattern说的是COLOR FILTER的结构, 分为两种: ...
- SRM 601(1-250pt,500pt)
DIV1 250pt 题意:有很多袋子,里面装有苹果和橘子(也可能没有),给出每个袋子里有多少个苹果,多少个橘子.如果每个袋子里含有水果的总数都不小于x个,则可以从每个袋子里都拿出x个水果(拿出苹果和 ...
- Parallel.Foreach的全部知识要点【转】
简介 当需要为多核机器进行优化的时候,最好先检查下你的程序是否有处理能够分割开来进行并行处理.(例如,有一个巨大的数据集合,其中的元素需要一个一个进行彼此独立的耗时计算). .net framewor ...
- string字符串转成16进制
package util; public class EscapeUnescape { public static String escape(String src) { int i; char j; ...
- jquery_EasyUI的学习
1 Accordion(可折叠标签) 1.1 实例 1.1.1 代码 <html> <head> <meta http-equiv="Content-Type& ...
- [AngularJS + Webpack] Requiring CSS & Preprocessors
Making your CSS modular is a difficult thing to do, but using Webpack makes this so much easier. By ...
- 手机相机ISO是什么
要说什么是ISO还要从传统胶片相机说起,ISO被 称为感光度,它是衡量传统相机所使用胶片感光速度的国际统一指标,其数值反映了胶片感光时的速度(其实是银元素与光线的光化学反应速率).而对于现在并不 使用 ...
- SQLLite 简介
[1] SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内 ...
- Qt 读写XML文件
1.读操作: QDomDocument doc( “mydocument " ); QFile file( "ccc.xml" ); if ( !file.open( I ...