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:
Input: word1 = "sea", word2 = "eat" Output: 2 Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Example 2:
Input: word1 = "leetcode", word2 = "etco" Output: 4
Constraints:
1 <= word1.length, word2.length <= 500
word1
and word2
consist of only lowercase English letters.This problem asks for the minimum number of deletions needed to make two strings identical. A dynamic programming approach efficiently solves this.
Approach:
The core idea is to find the longest common subsequence (LCS) of the two input strings. The number of deletions required is simply the sum of the lengths of the two strings minus twice the length of their LCS. This is because the characters in the LCS don't need to be deleted.
Dynamic Programming Table:
We construct a 2D table f
where f[i][j]
represents the length of the LCS of the first i
characters of word1
and the first j
characters of word2
.
Base Cases:
f[i][0] = 0
for all i
(LCS with an empty string is empty)f[0][j] = 0
for all j
(LCS with an empty string is empty)Recursive Relation:
word1[i-1] == word2[j-1]
, then f[i][j] = f[i-1][j-1] + 1
(extend the LCS)f[i][j] = max(f[i-1][j], f[i][j-1])
(take the longer LCS from either omitting the last character of word1
or word2
)Code Explanation (Python):
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
f = [[0] * (n + 1) for _ in range(m + 1)] # Initialize DP table
for i in range(1, m + 1):
for j in range(1, n + 1):
if word1[i - 1] == word2[j - 1]:
f[i][j] = f[i - 1][j - 1] + 1 # Extend LCS
else:
f[i][j] = max(f[i - 1][j], f[i][j - 1]) # Choose max LCS
lcs_length = f[m][n] # Length of LCS
return m + n - 2 * lcs_length # Total deletions
Time and Space Complexity:
word1
and word2
. This is due to the nested loops filling the DP table.The provided solutions in other languages (Java, C++, Go, TypeScript, Rust) follow the same dynamic programming algorithm, differing only in syntax and data structure usage. They all share the same time and space complexity.