一、类型描述
Two Sequences 的题目一般会提供两个sequence,一般问最大/最小、true/false、count(*)这几类的问题。
其中,Two Sequences的动态规划题目的四要素:
- state:dp[i][j] 一般表示 第一个sequence的前i个字符 和 第二个sequence的前j个字符 怎么怎么样。
- Initialization: 这类型动态规划一般初始化dp数组的方式是根据题目的含义初始化第一行和第一列。
- function:解决动态规划的function问题,就是找到当前待解决问题dp[i][j] 和 前面解决过的问(例如,dp[i - 1][j]、dp[i][j - 1])的之间的关系。
- answer:dp[s1.length()][s2.length()]
二、Leetcode题目举例
97. Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
题目中,提供了两个source string,一个目标string,问的也是true or false的问题,因此可以考虑two sequences的解法。
- 取状态函数为dp[i][j] 表示 s1的前i个字符 和 s2的前j个字符 能否组成 s3的前 i + j 个字符。
- dp[i][j] 和sub question之间的关系:
需要求的:dp[i][j]
之前求得结果的子问题:dp[i - 1][j]、dp[i][j - 1] .......
优先考虑能否从靠近dp[i][j]的子问题求解:可以发现,可以更具s3的前(i+j)个字符的最后一个字符 来自于 s1 还是 s2 来分类:
因此 dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
Java Solution:
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s3.length() != s1.length() + s2.length()) { return false; }
// state dp[i][j] 表示s1的前i个字符 和 s2的前j个字符 能否组成s3的前(i + j) 个字符
boolean[][] dp = new boolean[s1.length() + 1][s2.length() + 1];
// initialization
// dp[0][j] :s1的前0个字符 和 s2的前j个字符 能否组成 s3的前j个字符?
for (int j = 0; j < dp[0].length; j++) {
// 要判断两个字符串是否相等, 必须用字符串的equals方法来比较
dp[0][j] = s2.substring(0, j).equals(s3.substring(0, j));
//dp[0][j] = s2.substring(0, j) == s3.substring(0, j); // 比较的是两个字符串的reference,并不是实际是否相等,因此这么写是错的
}
// dp[i][0] 同理
for (int i = 0; i < dp.length; i++) {
dp[i][0] = s1.substring(0, i).equals(s3.substring(0, i));
//dp[i][0] = s1.substring(0, i) == s3.substring(0, i); // 错误
}
// function
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = (dp[i - 1][j] && (s1.charAt(i - 1) == s3.charAt(i + j - 1))) || (dp[i][j - 1] && (s2.charAt(j - 1) == s3.charAt(i + j - 1)));
}
}
// answer
return dp[s1.length()][s2.length()];
}
}
2. 1143. Longest Common Subsequence (Medium)
Given two strings text1 and text2, return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example1Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example2
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
题目分析:
- 题目提供了两个sequences,并且问的是最大的公共子序列的长度,问最大可以考虑two sequences的动态规划类型
- 题目的状态定义:dp[i][j] 定义为 序列1的前i个字符 和 序列2的前j个字符的最大公共子序列的长度。
- 题目的方程定义,即如何拆解为子问题:
dp[i][j] = ?
同样按照之前的经验,可以将dp[i][j] 拆解为两类问题: 第一类是最长公共子序列中有序列1的第i个字符(index:i - 1),另一列是没有这个字符
有的话:这个字符可能对上的是 序列2的前j个字符的最后一个(则dp[i][j] = dp[i - 1][j - 1] + 1),也可能对上的是 序列2 的前j - 1个字符中的某个(dp[i][j] = dp[i ][j - 1]
没有的话:dp[i][j] = dp[i - 1][j]
Java Solution
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
// state: dp[i][j] 表示 text1的前i个字符串 和 text2的前j个字符串
// 的最长公共子序列
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
// initialization
for (int i = 0; i < dp.length; i++) { dp[i][0] = 0; }
for (int i = 0; i < dp[0].length; i++) { dp[0][i] = 0; }
// function dp[i][j] = ?
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
dp[i][j] = Math.max((dp[i - 1][j - 1] + 1), Math.max(dp[i - 1][j], dp[i][j - 1]));
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[text1.length()][text2.length()];
}
}
3. 72. Edit Distance (Hard)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
You have the following 3 operations permitted on a word:a. Insert a character
b. Delete a character
c. Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
题目分析:
给两个two sequences,通过插入删除和替换将sequence1 转换为 sequence2,求最小的操作数。看到两个sequences和求最小的问题,可以想到参考two sequences的动态规划。
- 状态定义 dp[i][j] 表示 将序列1的前i个字符 转换为 序列2的前j个字符 需要的最小操作数。
- 状态之间的关系: dp[i][j] = ?
和上面的几个题目类似,可以根据序列1的最后一个字符来表示:
情况1: words1.charAt(i - 1) == words2.charAt(j - 1)
此时,如果保留序列1的最后一个字符,则dp[i][j] = dp[i - 1][j - 1],
如果不保留,则删除最后一个字符 dp[i][j] = dp[i - 1][j] + 1, 不需要替换的这种操作(因为肯定比保留底), 插入操作dp[i][j - 1] + 1
情况2: words1.charAt(i - 1) != words2.charAt(j - 1)
删除:dp[i][j] = dp[i - 1][j] + 1
替换:dp[i][j] = dp[i - 1][j - 1] + 1
插入:dp[i][j] = dp[i][j - 1]
class Solution {
public int minDistance(String word1, String word2) {
// state dp[i][j] 表示 word1的前i个字符 转化为 word2的前j个字符
// 需要的最短编辑距离
int[][] dp = new int[word1.length() + 1][word2.length() + 1];
// initialization
// 当i = 0,即编辑词word1为空,需要进行word2.length()次插入
for (int i = 0; i < dp.length; i++) { dp[i][0] = i; }
// 当j = 0,即目标词word2为空,需要进行word1.length()次删除
for (int i = 0; i < dp[0].length; i++) { dp[0][i] = i; }
// function dp[i][j] = ?
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
// word1.charAt(j - 1) 要 或者不要
dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j] + 1);
} else {
dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i - 1][j - 1]), dp[i][j - 1]) + 1;
}
}
}
return dp[word1.length()][word2.length()];
}
}
4. 115. Distinct Subsequences (Hard)
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:
There are 3 ways you can generate "rabbit" from S.
题目分析:
- 有两个String,求String1的不同的sub sequences 等于string2的总数(count(*)类型),考虑two sequences的动态规划。
- 状态定义:dp[i][j]表示 string1 的前i个字符 中 等于 string2 的前j个字符的不同sub sequences总数。
- 状态之间关系:
情况1: string1.charAt(i - 1) == string2.charAt(j - 1):
dp[i][j] = 包含string1的第i个:dp[i - 1][j - 1] + 不包含string的第i个:dp[i - 1][j]
情况2: 不等
dp[i][j] = dp[i - 1][j] , 应为string1的第i个如果有的话一定是最后一个(不能改变内部顺序),因此和string2的第j个如果不等,则一定不能取。
Java Solution
class Solution {
public int numDistinct(String s, String t) {
// state dp[i][j] 表示s的前i个字符中,与t的前j个字符相同的不同子串的个数
int[][] dp = new int[s.length() + 1][t.length() + 1];
// initialization 特别注意这道题的初始化条件
for (int i = 0; i < dp.length; i++) { dp[i][0] = 1; } // 什么都不1取算一种取法
// for (int i = 0; i < dp[0].length; i++) { dp[0][i] = 0; }
// function
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[s.length()][t.length()];
}
}
三、总结
这类型的问题的状态表示通常是第一个sequence的前i个 加上 第二个sequence的前j个 怎么怎么样
其状态之间的关系通常可以根据 结果中取或者不取第一个sequence的第i个字符分类讨论。