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. java作用域-转

    java中,针对不同的修饰词,类及其类中的方法.域都有不同的可见性.以下为针对java中可见性的几点总结. 1.java中的默认包(这个包是没有名称的),对于任何修饰词来说,其中的内容只能对其包内类为 ...

  2. The connection to adb is down, and a severe error has occured.

    启动android模拟器时.有时会报The connection to adb is down, and a severe error has occured.的错误.在网友说在任务管理器上把所有ad ...

  3. Hibernate入门学习(二)

    本文主要讲如何搭建Hibernate开发环境和简单实例. 一.搭建开发测试环境 1.1 下载Hibernate 从Hibernate官方网站上下载最新的Hibernate ORM,从Hibernate ...

  4. 【linux】which和whereis

    which和whereis都是查询命令的指令.区别的是: which能查询到命令所在位置: [root@andon tmp]# which ls alias ls='ls --color=auto' ...

  5. Neutron分析(2)——neutron-server启动过程分析

    neutron-server启动过程分析 1. /etc/init.d/neutron-server DAEMON=/usr/bin/neutron-server DAEMON_ARGS=" ...

  6. mvc checked=\"checked\"

    <input id="IsDiscount" type="checkbox" @(Model != null && Model.IsDis ...

  7. phpinfo详解

    php的很多信息都可以从phpinfo中获取,下面就详细了解下phpinfo的输出内容 1 php版本信息 第一行显示当前php版本 PHP Version 5.5.12 2 php.ini文件的位置 ...

  8. 成功移植SQLite3到ARM Linux开发板

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

  9. java工程师分享:我是如何自学成才的?

    原文:http://www.java800.com/peixun-79062115.html 我是10年河南工业大学的毕业生,当时我们专业许多学生都去报了java培训机构,去达内的都不少.我也想去培训 ...

  10. Mysql讲解数据库并发控制知识

    1.下载Mysql并安装,我喜欢不用安装的zip版,cd到bin目录下,先修改下mysql的密码. mysqladmin -u root -p password mysql ,第一次运行并修改mysq ...