classSolution: deffindLength(self, A: List[int], B: List[int]) -> int: # A --> j # B --> i ifnot A ornot B: return0 dp = [[0]*len(A) for i inrange(len(B))] res = 0 for j inrange(len(A)): if A[j] == B[0]: dp[0][j] = 1 res = max(res,dp[0][j]) for i inrange(len(B)): if B[i] == A[0]: dp[i][0] = 1 res = max(res,dp[i][0]) for i inrange(1,len(A)): for j inrange(1,len(B)): if B[i] == A[j]: dp[i][j] = dp[i-1][j-1] + 1 res = max(res,dp[i][j]) return res
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.
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.
Example 1:
Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2:
Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3:
Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.
Constraints:
1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
思路
dp[i][j] stands for length of LCS between text1 up to i and text2 up to j.
dp[i][j] = dp[i-1][j-1] + 1 if text1[i] == text2[j]
classSolution: deflongestCommonSubsequence(self, text1: str, text2: str) -> int: ifnot text1 ornot text2: return0 dp = [[0] * len(text1) for i inrange(len(text2))] ## text1 -->j; text2-->i flag = False for i inrange(len(text2)): if text2[i] == text1[0]: flag = True if flag: dp[i][0] = 1 flag = False for j inrange(len(text1)): if text1[j] == text2[0]: flag = True if flag: dp[0][j] = 1 for i inrange(1,len(text2)): for j inrange(1,len(text1)): if text2[i] == text1[j]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j],dp[i][j-1]) return dp[i][j]
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, wherein each step you can delete one character in either string.
Example 1:
Input: "sea", "eat" Output: 2 Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea". Note:
The length of given words won't exceed 500. Characters in given words can only be lower-case letters.
思路
这题实际上是求两个字符串的 Longest Common Subsequence. 然后用两个字符串的长度和减去 2*LCS. DP的思路与上题一样。
classSolution: defminDistance(self, word1: str, word2: str) -> int: ifnot word1 ornot word2: same = 0 else: dp = [[0] * len(word1) for i inrange(len(word2))] ## text1 -->j; text2-->i
flag = False for i inrange(len(word2)): if word2[i] == word1[0]: flag = True if flag: dp[i][0] = 1
flag = False for j inrange(len(word1)): if word1[j] == word2[0]: flag = True if flag: dp[0][j] = 1
for i inrange(1,len(word2)): for j inrange(1,len(word1)): if word2[i] == word1[j]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j],dp[i][j-1])
same = dp[i][j] returnlen(word1) + len(word2) - same*2
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If multiple answers exist, you may return any of them.
(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)
Example 1:
Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.
Note:
1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.