[LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
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"
Explanation: As shown below, there are 3 ways you can generate "rabbit" from S.
Output: 3
(The caret symbol ^ means the chosen letters)rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^
Example 2:
Input: S ="babgbag"
, T ="bag"
Explanation: As shown below, there are 5 ways you can generate "bag" from S.
Output: 5
(The caret symbol ^ means the chosen letters)babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^ 这个题目思路是用两个sequence的dynamic programming,mem[l1 + 1][l2 + 1] # mem[i][j]表示number of distinct subsequences of S[: i + 1] which equals T[: j + 1]
mem[i][j] = mem[i - 1][j] + mem[i - 1][j - 1] if s1[i - 1] == s2[j - 1] # 如果相等,可以选择配对或者不配对
mem[i - 1][j] if s1[i - 1] != s2[j - 1] # 如果不相等,就必须把最后一个舍弃掉 初始化: mem[i][0] = 1, mem[0][j] = 0 (j > 1) code
class Solution:
def disSubsequences(self, s1, s2):
l1, l2 = len(s1), len(s2)
mem = [[0] * (l2 + 1) for _ in range(l1 + 1)]
for i in range(l1 + 1):
mem[i][0] = 1
for i in range(1, l1 + 1):
for j in range(1, l2 + 1):
mem[i][j] = mem[i - 1][j] if s1[i - 1] != s2[j - 1] else mem[i - 1][j] + mem[i - 1][j - 1]
return mem[l1][l2]
[LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming的更多相关文章
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming
Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...
- [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 198. House Robber _Easy tag: Dynamic Programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
随机推荐
- fft,ntt
在被两题卡了常数之后,花了很久优化了自己的模板 现在的一般来说任意模数求逆1s跑3e5,exp跑1e5是没啥问题的(自己电脑,可能比luogu慢一倍) 当模数是$998244353,100453580 ...
- pandas设置值-【老鱼学pandas】
本节主要讲述如何根据上篇博客中选择出相应的数据之后,对其中的数据进行修改. 对某个值进行修改 例如,我们想对数据集中第2行第2列的数据进行修改: import pandas as pd import ...
- vue-cli 构建的项目中 如何使用less
vue-cli 构建的项目默认是不支持 less 的,需要自己添加. 首选,安装 less 和 less-loader ,在项目目录下运行如下命令 npm install less less-load ...
- python基础篇_006_面向对象
面向对象 1.初识类: # 定义一个函数,我们使用关键字 def """ def 函数名(参数): '''函数说明''' 函数体 return 返回值 "&qu ...
- BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)
题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...
- 69. x 的平方根
问题描述 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: ...
- 入门级----黑盒测试、白盒测试、手工测试、自动化测试、探索性测试、单元测试、性能测试、数据库性能、压力测试、安全性测试、SQL注入、缓冲区溢出、环境测试
黑盒测试 黑盒测试把产品软件当成是一个黑箱子,只有出口和入口,测试过程中只要知道往黑盒中输入什么东西,知道黑盒会出来什么结果就可以了,不需要了解黑箱子里面是如果做的. 即测试人员不用费神去理解软件里面 ...
- css 制作翻页布局
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- MongoDB 组合多个条件查询($and、$in、$gte、$lte)
一,问题描述 数据格式: id, timeStamp,count 条件1:查询 某个时间段 内的数据: timeStamp BETWEEN startTime AND endTime.比如 timeS ...
- HTML5_canvas 画布
<canvas></canvas> 画布 <canvas id="my_canvas" width="400" height=&q ...