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

  1. int num=;
  2. void countnum(string S, string T) {
  3. if(T.size()==)
  4. {
  5. num++;
  6. return;
  7. }
  8.  
  9. for(int i=; i<S.size(); i++)
  10. {
  11. if(S[i]==T[])
  12. {
  13. string s2 = S.substr(i+);
  14. string t2 = T.substr();
  15. countnum(s2, t2);
  16. }
  17.  
  18. }
  19. return;
  20. }
  21.  
  22. class Solution {
  23. public:
  24. int numDistinct(string S, string T) {
  25. countnum(S, T);
  26. return num;
  27. }
  28. };

方法二:用动态规划(DP)实现,需要的空间复杂度为O(N*M),对于大数据也可以很快处理。

  1. class Solution {
  2. public:
  3. int numDistinct(string S, string T) {
  4. vector<vector<int> > num(S.size()+,vector<int>(T.size()+,)); //num[i][j]表示T中的前j个字符构成的子字符串在S中的前i个字符中出现的次数,num[i][j]满足:
  5. S = " "+ S; //(1)若S[i]=T[j],则num[i][j] = num[i-1][j]+num[i-1][j-1];
  6. T = " "+ T; //(2)若S[i]!=T[j],则num[i][j] = num[i-1][j];
  7. num[][]=; //(3)若j>i,则num[i][j]=0。
  8. for(int i=; i<S.size(); i++)
  9. for(int j=; j<T.size(); j++)
  10. {
  11. if(j>i)
  12. {
  13. num[i][j]=;
  14. break;
  15. }
  16. if(S[i]==T[j])
  17. num[i][j] = num[i-][j] + num[i-][j-];
  18. else
  19. num[i][j] = num[i-][j];
  20. }
  21. return num[S.size()-][T.size()-];
  22.  
  23. }
  24. };

[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. page-object使用(2)---elements

    elements就是html元素下所有的标签.用page-object你可以找到并定位html页面下绝大多数的元素,这个文章列出了可定位的这些元素,生成的方法,和依据什么关键字来找到这些元素. BUT ...

  2. Bayer RGB和RGB Raw

    Bayer RGB和RGB Raw 对于SENSOR来说,Bayer RGB和RGB Raw两者的图象结构都是BG/GR的(Bayer pattern说的是COLOR FILTER的结构, 分为两种: ...

  3. SRM 601(1-250pt,500pt)

    DIV1 250pt 题意:有很多袋子,里面装有苹果和橘子(也可能没有),给出每个袋子里有多少个苹果,多少个橘子.如果每个袋子里含有水果的总数都不小于x个,则可以从每个袋子里都拿出x个水果(拿出苹果和 ...

  4. Parallel.Foreach的全部知识要点【转】

    简介 当需要为多核机器进行优化的时候,最好先检查下你的程序是否有处理能够分割开来进行并行处理.(例如,有一个巨大的数据集合,其中的元素需要一个一个进行彼此独立的耗时计算). .net framewor ...

  5. string字符串转成16进制

    package util; public class EscapeUnescape { public static String escape(String src) { int i; char j; ...

  6. jquery_EasyUI的学习

    1 Accordion(可折叠标签) 1.1 实例 1.1.1 代码 <html> <head> <meta http-equiv="Content-Type& ...

  7. [AngularJS + Webpack] Requiring CSS & Preprocessors

    Making your CSS modular is a difficult thing to do, but using Webpack makes this so much easier. By ...

  8. 手机相机ISO是什么

    要说什么是ISO还要从传统胶片相机说起,ISO被 称为感光度,它是衡量传统相机所使用胶片感光速度的国际统一指标,其数值反映了胶片感光时的速度(其实是银元素与光线的光化学反应速率).而对于现在并不 使用 ...

  9. SQLLite 简介

    [1] SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内 ...

  10. Qt 读写XML文件

    1.读操作: QDomDocument doc( “mydocument " ); QFile file( "ccc.xml" ); if ( !file.open( I ...