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 ...
随机推荐
- WPF中,DataGrid最左边多出一行的解决方案
这种情况下,请在DataGrid的属性里加上这个属性: RowHeaderWidth="0" 必须赋值为0,不能不赋值,也不能赋其他值. 问题解决.
- Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) A Amusing Numbers (数学)
其实挺简单的.先直接算出之前已经排在k这个数前面的数字.比如543是三位的,那么100~543都是可以的,两位的10~54. 如果还需要往前面补的话,那么依次考虑1000~5430,5430是上界不能 ...
- Tarjan的学习笔记 求割边求割点
博主图论比较弱,搜了模版也不会用... 所以决心学习下tarjan算法. 割点和割边的概念不在赘述,tarjan能在线性时间复杂度内求出割边. 重要的概念:时间戟,就是一个全局变量clock记录访问结 ...
- 使用Python-OpenCV向图片添加噪声(高斯噪声、椒盐噪声)
在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声.Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现. 代码: import numpy as np impo ...
- 斐讯k2p 月光银 硬件版本A2-软件版本22.7.8.5 刷官改系统
Mark https://huabuyu.net/斐讯k2p%20月光银%20硬件版本A2-软件版本22.7.8.5%20刷官改系统.html 详细资源推荐:恩山论坛 https://www.righ ...
- python 函数内使用全局变量
x = def change_global(): global x x = x + change_global() print(x) result: 2
- 03_4_this关键字
03_4_this关键字 1. this关键字 在类的方法定义中使用的this关键字代表使用该方法的对象的引用. 当必须指出当前使用方法的对象是谁时要使用this. 有时使用this可以处理方法中成员 ...
- 【转】Matlab的regionprops详解
matlab函数_连通区域 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下 ...
- Ajax原生代码
Ajax传数据有两种方式:get/post.下面是前台的get/post方式的代码. //------------原生--------- function AjaxGET(){ //第一步 调用Aja ...