Given a string S and a string T, count the number of distinct subsequences of S which equals T.

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).

Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation: As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters) rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

题意:

给定字符串S和T,求字符串S中有多少种不同的subsequences能完全match字符串T

思路:

这是一道高频dp题,需要熟练掌握

注意区分substring(要求连续)和subsequence(不要求连续)

进一步理解题意,

S : rabbbit  删掉第一个‘b’ , 可以跟T完全match
S : rabbbit  删掉第二个‘b’ , 可以跟T完全match
S : rabbbit  删掉第三个‘b’ , 可以跟T完全match

return 3 (number of distinct subsequences)

用dp[i][j]来记录S的子序列跟T匹配的个数

初始化的时候,除了要处理dp[0][0],还要条件反射的习惯性思考是否需要预处理第一个row : dp[0][j] 和第一个col:dp[i][0]

    T =    0 "r a b b i t"
0 1 0 0 0 0 0 0
S = "r 1 1 0 0 0 0 0
a 1 1 1 0 0 0 0
b 1 1 1 1 0 0 0
b 1 1 1 2 1 0 0
b 1 1 1 3 ?
i 1 1
t 1 1

显然,

对于是否需要预处理第一个row : dp[0][j], 发现当S为空,T为任意字符都不可能跟S匹配。 对于dp[0][j]不需要多做处理,只保留defalut值为0即可

对于是否需要预处理第一个col:dp[i][0], 发现当T为空,S的当前字符都可以partition into two subsequences :  " " + 当前字符, 所以dp[i][0] = 1

对于dp[i][j],

若 s.charAt(i-1) ! = t.charAt(j-1)  则S当前的字符必须删掉,再看S和T是否匹配: dp[i][j] = dp[i-1][j]

若 s.charAt(i-1) == t.charAt(j-1)  则S当前的字符要么删掉:dp[i][j] = dp[i-1][j] ; 要么保留:dp[i][j] = dp[i-1][j-1]

代码

 class Solution {
public int numDistinct(String s, String t) {
int[][] dp = new int[s.length() +1 ][t.length() + 1];
dp[0][0] = 1;
for(int i = 1; i <= s.length() ; i++){
dp[i][0] = 1;
}
for(int i = 1; i <= s.length() ; i++){
for(int j = 1; j<=t.length(); j++){
if( s.charAt(i-1) != t.charAt(j-1) ){
dp[i][j] = dp[i-1][j];
}else{
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
}
}
}
return dp[s.length()][t.length()];
}
}

[leetcode]115. Distinct Subsequences 计算不同子序列个数的更多相关文章

  1. [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 ...

  2. Leetcode 115 Distinct Subsequences 解题报告

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

  3. 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 ...

  4. 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 ...

  5. Leetcode#115 Distinct Subsequences

    原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...

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

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

  7. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

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

  9. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 6.28笔记-servlet3.0注解配置、文件上传、过滤器、监听器

    一.servlet3.0注解配置 使用javaEE6.0 支持servlet3.0 value的值就是访问路径 urlPatterns的值也是访问路径 @WebServlet(name="D ...

  2. 【Linux_Unix系统编程】chapter6 进程

    chapter6 进程 重点关注进程虚拟内存的布局及内容.6.1 进程和程序 进程(process)是一个可执行程序(program)的实例. 程序是包含了一系列信息的文件,这些信息描述了如何在运行时 ...

  3. spring的Ioc容器与AOP机制

    为什么要使用Spring的Ioc容器? 1.首先,spring是一个框架,框架存在的目的就是给我们的编程提供简洁的接口,可以使得我们专注于业务的开发,模块化,代码简洁,修改方便. 通过使用spring ...

  4. php 七种数据类型介绍

    PHP有7个数据类型.七个类型: 字符串, 整数, 浮动, 布尔, 数组, 对象, 资源. 字符串 字符串保持字符,如“一”.“abc”,“www.manongjc.com”等.PHP字符串是区分大小 ...

  5. Python 实现双向链表(图解)

    原文:https://blog.csdn.net/qq490691606/article/details/49948263 git 路径 https://github.com/wangpanjun/d ...

  6. gz文件最后四位检测

    [root@node-0 ~]# ll -rw-r--r--  1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...

  7. Java 中 HashMap 初始化时赋值

      1.HashMap 初始化的文艺写法 HashMap 是一种常用的数据结构,一般用来做数据字典或者 Hash 查找的容器.普通青年一般会这么初始化:HashMap<String, Strin ...

  8. ckeditor源码编辑模式,添加style、javascript内容丢失的解决

    我使用ckeditor 我在编辑的使用源码编辑,保存内容包含javascript.style标签的时候,数据库中有javascript.style标签 , 输入到页面也可以执行,但是我再次编辑的时候就 ...

  9. RAD 10 C++Builder的bug

    C++Builder的bug 修改一行代码,F9会报错.要clear工程重新完整编译才可以. 新建空白工程是好的. restart computer ok!!! 2)fdquery like this ...

  10. 笔记本 T450的鼠标经常不灵

    T450的鼠标经常不灵,鼠标总感觉有延迟. 换了鼠标也是这样. 有人反应说是USB断电, 使用的是省电模式,在设备管理>鼠标>电源选项>节电模式 勾去掉就可以了, 但是我的节点模式是 ...