- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: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
.
Asubsequence 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]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
所以现在我有一个像这样的字符串列表{a, b, c, d, Justin, Connor, BYE1, BYE2} 现在我正在尝试用子序列 (0,3) 中的“BYE”过滤掉列表中的字符串。但是,如果我
我不知道在这里问这个问题是否合适,如果不合适,请见谅。 我得到了一个序列 ALPHA,例如: A B D Z A B X 我得到了 ALPHA 的子序列列表,例如: A B D B D A B D Z
优化 O(n^2)算法到O(n log n) . 问题陈述 给定数组 A的 n正整数。将数组分成长度不大于k的连续子序列使得每个子序列的最大值之和最小。这是一个例子。 如果n = 8和 k = 5和数
对于大多数 Swift Collections , 索引 Collection's SubSequence与底座兼容 Collection . func foo(_ buffer: T) -> T.I
我正在尝试解决 problem 的变体我之前问过: Given a string of parentheses (length <= 1,000,000) and a list of range qu
题目地址:https://leetcode.com/problems/distinct-subsequences/description/ 题目描述 Given a string S and a
比如说,String str = "hello world";要,打招呼,我们可以使用 str.subSequence(0, 5)。如果它是一个从 0 开始的索引字符串,那么为什么我们不把 str.s
所以我读了this answer和 this answer关于 subSequence() 和 subString() 之间的区别,我知道两者之间的唯一区别是返回类型。事实上,subSequence(
题目地址:https://leetcode.com/problems/increasing-triplet-subsequence/description/ 题目描述: Given an unso
题目地址:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目描述 Given an unsor
题目地址:https://leetcode.com/problems/longest-palindromic-subsequence/description/ 题目描述 Given a strin
我在使用 Java 和 Android Studio 时遇到问题;以下代码是一个退格按钮: else if(view == btnBackspace){ int expressionLengt
import requests for i in range(3): g = requests.get('http://some-url/') print "request done"
我已经阅读了 LCS 问题的解决方案。但是现在有一个最长相似子序列问题:序列 C 是两个序列 A、B 的相似子序列当且仅当 C 是 A 的子序列并且我们最多可以替换 C 中的 K 个元素使得 C 是
我将复习在寻找两个等长字符串的最长公共(public)子序列的上下文中讨论动态规划的笔记。有问题的算法输出长度(不是子字符串)。 所以我有两个字符串,比如说: S = ABAZDC,T = BACBA
我们可以用DP(动态规划)找到两个字符串的LCS(最长公共(public)子序列)。通过跟踪 DP 表,我们可以获得 LCS。但是,如果存在不止一个濒海战斗舰,我们如何获得所有的濒海战斗舰呢? 例子:
N4567 标准禁止对先前在条件中声明的名称进行某些类型的重新声明,如下所示——根据标准(§3.3.3/4): Names declared in the for-init-statement, th
题目地址:https://leetcode.com/problems/longest-uncommon-subsequence-i/description/ 题目描述 Given a group
题目地址:https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 题目描述 Give
题目地址:https://leetcode.com/problems/distinct-subsequences-ii/description/ 题目描述 Given a string S, co
我是一名优秀的程序员,十分优秀!