【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/distinct-subsequences/description/
题目描述
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).
Example 1:
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
^^^ ^^^
Example 2:
Input: S = "babgbag", T = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^
题目大意
求S中有多少个子序列等于T。
解题方法
动态规划
这个题一看就是DP。向字符串序列问题确实有很多都是用DP求解的。
设dp数组dp[i][j]表示S的前j个字符是T的前i个字符的子序列的个数为dp[i][j]。
那么有dp[0][*] == 1,因为这个情况下,只能使用s的空字符串进行匹配t。
如果s[j - 1] == t[i - 1],那么,dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1],原因是t的前j个字符可以由s的前[i - 1]个字符和t的前[j - 1]个匹配的同时最后一个字符匹配,加上s的前[j - 1]个字符和t的前[i]个字符匹配同时丢弃s的第[j]个字符。
如果s[j - 1] != t[i - 1],那么dp[i][j] = dp[i][j - 1],因为只能是前面的匹配,最后一个字符不能匹配,所以丢弃了。
class Solution:
def numDistinct(self, s, t):
"""
:type s: str
:type t: str
:rtype: int
"""
M, N = len(s), len(t)
dp = [[0] * (M + 1) for _ in range(N + 1)]
for j in range(M + 1):
dp[0][j] = 1
for i in range(1, N + 1):
for j in range(1, M + 1):
if s[j - 1] == t[i - 1]:
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]
else:
dp[i][j] = dp[i][j - 1]
return dp[-1][-1]
日期
2018 年 11 月 19 日 —— 周一又开始了
【LeetCode】115. Distinct Subsequences 解题报告(Python)的更多相关文章
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 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 ...
随机推荐
- xshell的快捷复制粘贴设置
今天试着用xshell连接Linux,运行一些命令的时候想快点复制粘贴实现效率,却发现还要右键选择复制,再右键选择粘贴,很是麻烦. 看了一下xshell的设置,其实可以自己设置成快捷方式 以xshel ...
- IO流中的字符输入输出流及try...catch处理流处理中的异常
使用字节流读取中文的问题 import java.io.FileInputStream; import java.io.IOException; /* 使用字节流读取中文文件 1个中文 GBK:占用两 ...
- 08 eclipse配置JDK
eclipse配置JDK1.8 一.打开eclipse:Window>>Preferences: 二.搜索:"jdk",并点击右侧的"Add": 三 ...
- 5 — springboot中的yml多环境配置
1.改文件后缀 2.一张截图搞定多环境编写和切换
- 数据集成工具—FlinkX
@ 目录 FlinkX的安装与简单使用 FlinkX的安装 FlinkX的简单使用 读取mysql中student表中数据 FlinkX本地运行 MySQLToHDFS MySQLToHive MyS ...
- 日常Java 2021/11/15
Applet类 每一个Applet都是java.applet Applet类的子类,基础的Applet类提供了供衍生类调用的方法,以此来得到浏览器上下文的信息和服务.这些方法做了如下事情: 得到App ...
- 日常Java 2021/10/27
java HashMap HashMap是一个散列表,它存储的内客是键值对(key-value)映射.HashMap实现了Map.接口,根据键的HashCode值存储数据,具有很快的访问速度,最多允许 ...
- tomcat 8 内存优化
在Linux环境下设置Tomcat JVM,在/opt/tomcat/bin/catalina.sh文件中找到"# ----- Execute The Requested Command&q ...
- Mybatis中 SIMPLE、REUSE、BATCH的区别
Executor分成两大类,一类是CacheExecutor,另一类是普通Executor. 普通类又分为: ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情.它为每个语句的执行 ...
- spring的核心容器ApplicationContext
//bean.xml配置文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns=" ...