Distinct Subsequences

OJ: https://oj.leetcode.com/problems/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.

思想:动态规划。 D[i][j] = D[i][j-1] + (T[i-1] == S[j-1] ? D[i-1][j-1] : 0);

// DP: D[i][j] = D[i][j-1] + (T[i-1] == S[j-1] ? D[i-1][j-1] : 0);
class Solution {
public:
int numDistinct(string S, string T) {
int m = T.length();
int n = S.length();
if(!m) return 1;
if(m > n) return 0;
vector<vector<int> > D(m+1, vector<int>(n+1));
for(int i = 1; i <= m; ++i) D[i][0] = 0;
for(int i = 0; i <= n; ++i) D[0][i] = 1;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
D[i+1][j+1] = D[i+1][j] + (T[i] == S[j] ? D[i][j] : 0);
return D[m][n];
}
};

改进后:空间复杂度 O(T.size()).

class Solution {
public:
int numDistinct(string S, string T) {
int m = T.length();
vector<int> num(m+1, 0); // num[i] is distinct numbers of T[1,...,i] in string S
num[0] = 1;
for(int i = 1; i <= S.length(); ++i)
for(int j = min(i, m); j >= 1; --j) // key to notice.
if(T[j-1] == S[i-1]) num[j] += num[j-1]; // num[j-1] 为上次字符串时,T[1,...,j-1] 的 distinct numbers。
return num[m];
}
};

30. Distinct Subsequences的更多相关文章

  1. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  3. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  4. Leetcode Distinct Subsequences

    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

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  6. [Leetcode][JAVA] Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  7. Distinct Subsequences Leetcode

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. 【leetcode】Distinct Subsequences(hard)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

随机推荐

  1. net cookie操作

    写入方法: HttpCookie cookie = new HttpCookie("id"); cookie.Value=cookieValue; cookie.Expires = ...

  2. xshell 5连接linux服务器的技巧

    1.用sttp 方式连接服务器,命令识别不了,用ssh方式才能有效

  3. 转: Ext拖拽分析

    整个Ext架构中组件是其重要的组成部分,除了少部分(如树的结点)的界面表现元素不是在这样的一个体系中,大部分的页面表现元素都被绑定在这个体系之中,下面从这个体系的最底层即在这个继承体系的最高层进行研究 ...

  4. java.io.IOException: ORA-22920: 未锁定含有 LOB 值的行

         究其原因是因为没有锁定要更新的行记录.将 mysql="select filebody from filelist where filename=?"中的SQL语句加上 ...

  5. php日期时间函数

    1,年-月-日echo date('Y-m-j');2007-02-6echo date('y-n-j');07-2-6大写Y表示年四位数字,而小写y表示年的两位数字:小写m表示月份的数字(带前导), ...

  6. smarty模板基础

    将前台后台隔离,前台控制显示,后台控制逻辑/内容,与cms类似 原理: 用户访问text.php页面,后台调用类smarty.class.php显示静态模板;

  7. js 编号生成器

    编号生成器 前缀: 后缀: 位数: 连续数字 随机字符 范围: ~ 过滤字符: 多个使用,号分割 0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLM ...

  8. php 添加redis扩展(二)

    php代码操作redis 1.连接 <?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 63 ...

  9. android 插件化开发 开源项目列表

    开源的插件化框架 Qihoo360/DroidPlugin CtripMobile/DynamicAPK mmin18/AndroidDynamicLoader singwhatiwanna/dyna ...

  10. 剑指offer系列24---数组中重复的数字

    * [24] * [题目]在一个长度为n的数组里的所有数字都在0到n-1的范围内. * 数组中某些数字是重复的,但不知道有几个数字是重复的. * 也不知道每个数字重复几次. * 请找出数组中任意一个重 ...