[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 ...
随机推荐
- php实现socket
一.Socket 简介 1.socket只不过是一个数据结构. 2.使用这个socket数据结构去开始一个客户端和服务器之间的会话. 3.服务器是一直在监听准备产生一个新的会话.当一个客户端连接服务器 ...
- Spark学习笔记4:数据读取与保存
Spark对很多种文件格式的读取和保存方式都很简单.Spark会根据文件扩展名选择对应的处理方式. Spark支持的一些常见文件格式如下: 文本文件 使用文件路径作为参数调用SparkContext中 ...
- Hadoop2.0的基本构成总览
Hadoop1.x和Hadoop2.0构成图对比 Hadoop1.x构成: HDFS.MapReduce(资源管理和任务调度):运行时环境为JobTracker和TaskTracker: Hadoop ...
- ECCV 2018 | 旷视科技提出GridFace:通过学习局部单应变换实现人脸校正
全球计算机视觉三大顶会之一 ECCV 2018(European Conference on Computer Vision)即将于 9 月 8 -14 日在德国慕尼黑拉开帷幕,旷视科技有多篇论文被此 ...
- ECCV 2018 | UBC&腾讯AI Lab提出首个模块化GAN架构,搞定任意图像PS组合
通常的图像转换模型(如 StarGAN.CycleGAN.IcGAN)无法实现同时训练,不同的转换配对也不能组合.在本文中,英属哥伦比亚大学(UBC)与腾讯 AI Lab 共同提出了一种新型的模块化多 ...
- 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...
- selenium+python自动化95-弹出框死活定位不到
前言 部分小伙伴学了selenium的alert后,就不管啥弹出框都去用alert,这是行不通的,看到弹出框,先要确定是不是alert,是才能用,不是的话肯定不能用. 有些弹出框是div层,这种跟平常 ...
- python入门-异常
1 报错的例子 print(5/0) 2跳过报错的例子 try: print(5/0) except ZeroDivisionError: print("You can't divide b ...
- python入门-文件
1 读取文件 with open('1.txt') as file_ojbect: contents = file_ojbect.read() print(contents.rstrip()) wit ...
- hive整合hbase
Hive整合HBase后的好处: 通过Hive把数据加载到HBase中,数据源可以是文件也可以是Hive中的表. 通过整合,让HBase支持JOIN.GROUP等SQL查询语法. 通过整合,不仅可完成 ...