Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences
Total Accepted: 38466 Total Submissions: 143567My Submissions
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.
class Solution(object):
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
num=[0]
self.CountSubsequence(s,t,0,0,num)
return num[0]
def CountSubsequence(self,father_sequence,child_sequence,index_father,index_child,num):
#print(index_father,index_child)
len_father=len(father_sequence)
len_child=len(child_sequence)
if index_child==len_child:
num[0]+=1
#print("匹配到了相同的")
else:
#print("进入迭代")
for i in range(index_father,len_father):
if father_sequence[i]==child_sequence[index_child]:
self.CountSubsequence(father_sequence,child_sequence,i+1,index_child+1,num)
#这里num是一个列表,可以从外部访问的,所以不需要return
方法二:DP(Dynamic Programming, 动态规划)
此处参考陆草纯的解题报告将问题转化为“二维地图走法问题”。
我觉得他在文章里对转化为“二维地图走法问题”说明的不清楚:
疑问一:为何走的时候只能“对角线走”和“向右向下走”,不能“向下向右走”。
疑问二:为何字符判断相等时,是“对角线走”和“向右向下走”相加;而字符不等时,只能“向右向下走”。
经过自己的思考,我来说一下我的理解:
一个子字符串t',一个父字符串s',两者一点一点相加。最终子字符串的长度加到T的长度,父字符串的长度加到S的长度。
当字符不等时,也就是说,父字符串s‘中新加的元素s'[i]无法对走法有贡献,所以可以删掉,于是就变成了“向右向下走”
字符相等时,父字符串s'中新加的元素s'[i]对走法有贡献,所以对角线是可以取的;同时“向右向下走”(即删掉s'[i])也是可行的;由于两者是不同的走法,自然要相加。
显然,DP的思路是从0开始一点一点增加子字符串的长度,最终达到我们想要匹配的字符串长度。显然不能减少字符串t'的长度。
大家画个图就明白了,以s' 为纵轴,t'为横轴。下面直接上AC的python代码:
class Solution(object):
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
#s is father_sequence
#t is child_sequence
len_father=len(s)
len_child=len(t)
dp=[[0 for i in range(len_child)] for j in range(len_father)]
if len_father==0 or len_child==0:
result=0
else:
#dp=[[0 for i in range(len_child)] for j in range(len_father)]
if s[0]==t[0]:
dp[0][0]=1
for i in range(1,len_father):
dp[i][0]=dp[i-1][0]
if s[i]==t[0]:
dp[i][0]+=1 for i in range(1,len_father):
for j in range(1,len_child):
if i>=j:
if s[i]==t[j]:
dp[i][j]=dp[i-1][j-1]+dp[i-1][j]
else:
dp[i][j]=dp[i-1][j]
result=dp[len_father-1][len_child-1]
return result
Leetcode 115 Distinct Subsequences 解题报告的更多相关文章
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 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 不同的子序列
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 ----- 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 计算不同子序列个数
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
原题地址 转化为求非重路径数问题,用动态规划求解,这种方法还挺常见的 举个例子,S="aabb",T="ab".构造如下地图("."表示空位 ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- IIS 7.0的根文件(applicationHost.config)位置及说明
位置 C:\Windows\System32\inetsrv\config\applicationHost.config 说明 https://www.microsoft.com/taiwan/tec ...
- java面试题(杨晓峰)---第七讲谈谈int和integer有什么区别?
理解装箱和拆箱的过程. 对象由三部分组成:对象头,对象实例,对齐填充. 对象头:一般是十六个字节,分两部分,第一部分:哈希码,锁状态标志,线程持有的锁,偏向线程id,gc分代年龄等,第二部分是类型指针 ...
- ProtoBuff3 unity_TCP网络发包解包&&消息订阅
using Google.Protobuf; //using Google.Protobuf.Examples.AddPerson; using Google.Protobuf.WellKnownTy ...
- MyLinkedList
/** * 节点类 * @author JP * */ class Node { Object value;//节点元素值 Node pre;//上一个节点 Node next;//下一个节点 pub ...
- 树莓派 - 修改pi账号密码,开启root账号
1.修改PI账号的密码 password pi 2.开启root账号 树莓派使用的Linux是debian系统,所以树莓派启用root和debian是相同的. debian里root账户默认没有密码, ...
- 《队长说得队》第九次团队作业:Beta冲刺与验收准备
项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十三 团队作业9:Beta冲刺与团队项目验收 团队名 ...
- setTimeout,clearTimeout的一些好用法
if(hidden != 1){ $.ui.showMask(); var _aaa = setTimeout(function(){ $.ui.hideMask(); },5000); } //be ...
- 使用struts2实现文件上传与下载功能
这个问题做了两天,在网上找了很多例子,但是还有一些功能没有实现,暂时先把代码贴出来,以后在做这方面的功能时在修改 文件上传: 一开始我在网上找到基于servlet+jsp环境写的文件上传,但是在将页面 ...
- JTT808、JTT809、JTT796、JTT794、JTT1077、JTT1078区别与交通部道路运输车辆卫星定位系统部标标准大全下载地址
部标JT/T808协议.JT/T809协议.JT/T796标准.JT/T794标准的区别,他们是基于不同的通信场景,不同的通信对象,不同的设计目的和目标而制定出来的.首先要知道这些标准的全称是什么意思 ...
- cocos2x (c++/lua) spine 文件的预加载
在之前,笔者写过一编博客,通过lua在加载场景加载spineAnimation动画精灵,保存在table中,然后在游戏中创建动画精灵时,提取加载好的spineAnimaiton中的 spSkeleto ...