583. Delete Operation for Two Strings
Description
Given two strings word1
and word2
, return the minimum number of steps required to make word1
and word2
the same.
In one step , you can delete exactly one character in either string.
Example 1:
Example 2:
Constraints:
1 <= word1.length, word2.length <= 500
word1
andword2
consist of only lowercase English letters.
Tags
Dynamic Programming, String
Solution
双串LCS问题。按本题操作后得到的结果就是最长公共子序列,因此删除掉的字符数目就是两字符串长度和减去2倍LCS。
Follow the instruction and the processed 2 strings we obtained are ought to the same as that in LeetCode 1143. Longest Common Subsequence. Thus, the result is equal to len(word1) + len(word2) - 2*lcs
.
Complexity
Time complexity:
Space complexity:
Code
Last updated
Was this helpful?