[leetcode]115. Distinct Subsequences 计算不同子序列个数
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 计算不同子序列个数的更多相关文章
- [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 ...
- 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 ----- 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
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划
Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- MTR追踪的好工具
yum install mtr 或者win下的winmtr 直接可以统计.
- IntelliJ IDEA小问题解决方法------(持续更新)
1:IDEA运行时报错提示“找不到或无法加载主类”:在确保IDEA开发环境无误后->file->invalidate Cache/restart,之后再重新build.问题解决. 2.如何 ...
- css3网站收集
把群里大家推荐的网站做了下收集,等有时间了研究下 1.http://icomoon.io/app/ 这个网站用来生成跟导出字体图标的,自带的图标种类很多很丰富,基本够用了,不过你也可以自己设计,然后 ...
- 小朋友学Java(1):Mac系统安装JDK
1 打开终端 方法可以参考http://blog.csdn.net/haishu_zheng/article/details/73410594 2 在终端输入 java -version,提示没有Ja ...
- 代码生成器 CodeSmith 的使用(二)
在第一篇中,简单的介绍了 CodeSmith 的使用方法,这次做一个生成简单的数据库字段属性的模板.以下只粘贴主要的代码片段. <%-- Name: Copyright © Sun 2013-2 ...
- 分布式tensorflow
分布式Tensorflow Tensorflow的一个特色就是分布式计算.分布式Tensorflow是由高性能的gRPC框架作为底层技术来支持的.这是一个通信框架gRPC(google remote ...
- 框架之Tornado(简单介绍)
引言 回想Django的部署方式 以Django为代表的python web应用部署时采用wsgi协议与服务器对接(被服务器托管),而这类服务器通常都是基于多线程的,也就是说每一个网络请求服务器都会有 ...
- uva-10420-排序
根据国家名字统计人数,然后排序 #include<stdio.h> #include<iostream> #include<queue> #include<m ...
- B站上的一个MATLAB与神经网络的视频,捡漏
▶ av15514817.这里集中了一些从视频中学到的散点. ▶ 语句 "edit + 函数名" 可以打开部分内置函数的源代码.非公开的源代码这会打开一个全是注释的文档. ▶ 函数 ...
- node 把前台传来的base64码转成图片存放
最近做个人网站头像修改用到了,在做头像修改,先做了一个图片切割,只需要上传你选中部分, 如图 这种需求 应该还是会遇到的, http://pan.baidu.com/s/1boVkn1t 这是裁剪图片 ...