[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 ...
随机推荐
- Accumulation Degree
#include<cstdio> #include<cstring> #define INF 0x7fffffff using namespace std; ; inline ...
- HTTPS开发(SSL--用Tomcat服务器配置https双向认证)
准备工作: 1.windows+R cmd 打开命令窗口 2.输入:cd C:\Program Files\Java\jdk1.7.0_80\bin 进入路径找到keytool工具 为服务器生成证书 ...
- 2019-2-20Sqlserver数据库中char、varchar、nchar、nvarchar的区别及查询表结构
varchar 和 nvarchar区别: varchar(n)长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字 ...
- PBRT笔记(3)——KD树
茎节点与叶子节点 茎节点与叶子节点皆适用KdAccelNode来表示 注意:这里使用了匿名union union有个特性:内部类型共用一段内存,且大小为内部最大类型的大小. struct KdAcce ...
- Android的Activity组件
本章主题是Activity组件:Activity是Android四大组价之一,其重要地位自然不用说.“Activity是应用程序中可见的交互组件的基类,大致上等同于传统桌面应用个程序开发中的窗体.”( ...
- Python assert 断言函数
http://blog.csdn.net/hunyxv/article/details/52737339 使用assert断言是学习python一个非常好的习惯,python assert 断言句语格 ...
- HBase scan 时 异常 ScannerTimeoutException 解决
org.apache.Hadoop.hbase.client.ScannerTimeoutException: 60622ms passed since the last invocation, ti ...
- IDEA_Springboot启动Tomcat报错_APR
使用idea新建一个Springboot项目 环境:idea+jdk8 启动异常如下: An older version [1.2.14] of the APR based Apache Tomcat ...
- Java 中的字符串与 []byte 字节数组
一.字符串 1.比较 String.HashSet.List 中的 contains 方法 其中, String.List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n).而 Has ...
- MySQL自动备份shell脚本
在数据库的日常维护工作中,除了保证业务的正常运行以外,就是要对数据库进行备份,以免造成数据库的丢失,从而给企业带来重大经济损失.通常备份可以按照备份时数据库状态分为热备和冷备,按照备份数据库文件的大小 ...