[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 ...
随机推荐
- [UE4]C++ string的用法和例子
使用场合: string是C++标准库的一个重要的部分,主要用于字符串处理.可以使用输入输出流方式直接进行操作,也可以通过文件等手段进行操作.同时C++的算法库对string也有着很好的支持,而且st ...
- 常用Java程序片段
1.改变数组的大小 package com.js.ai.modules.jsa.test; public class Testxf { private static Object resizeArra ...
- MySQL 安装方法
所有平台的Mysql下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. Linux/UNIX上安装Mysql Linux平台上推荐使用RP ...
- mysql sleep连接过多解决办法
睡眠连接过多,会对mysql服务器造成什么影响? 严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃. 造成睡眠连接过多的原因? 1. 使用了太多持久连接(个人觉得,在高并 ...
- 如何决定Web应用的线程池大小
线程池(Thread Pool)在Web应用中线程池的大小决定了在任何一个时间点应用可以处理请求的并发数.如果一个系统收到的请求数超过了线程池的大小,那么超出的请求要么进入等待队列要么被拒绝.请注意, ...
- 好用的 FTP 软件之 FileZilla 技巧教程
FTP 软件之 FileZilla教程 使用教程参考:http://163.26.161.1/~yilinteacher/wwwict/flash/FileZilla.swf (1)如何设置传输完成后 ...
- 修改IP
查看系统版本 [root@host ~]# cat /etc/issueCentOS release 6.5 (Final)Kernel \r on an \m [root@host ~]# cat ...
- java求两个数百分比,精确到指定位数
// 获取百分比,不带小数点 private String getPercentage(String num, String total){ NumberFormat numberFormat = N ...
- apache http get 和 post 请求
1.首先要把jar依赖进项目 <dependency> <groupId>org.apache.httpcomponents</groupId> <artif ...
- JDK9-模块化系统
Content 0. 实例 0.1 使用命令行编写和运行模块程序 0.1.1 设置目录 0.1.2 编写源代码 0.1.3 编译 0.1.4 打包模块代码 0.1.5 运行程序 0.2 使用eclip ...