Delete Operation for Two Strings
Delete Operation for Two Strings is LeetCode 583 (Medium). You get two lowercase words, word1 and word2. One step deletes exactly one character from either word. Return the fewest steps that make the two words equal.
- Deleting is the only move: no inserting and no replacing.
- Each word has at most 500 letters, so a table with one cell per pair of positions (about 250,000 cells) is fast enough.
- 1 <= word1.length, word2.length <= 500
- word1 and word2 consist of only lowercase English letters.
Intuition
Stop thinking about what to delete and look at what is left. After the deletions both words are the same string, and it was made by keeping some letters of word1 in order and some letters of word2 in order. So what is left is a common subsequence of the two words.
Fewer deletions means more letters kept, so keep the longest common subsequence (LCS). Its letters survive in both words, and every other letter goes.
So the minimum deletions to make two strings equal is m + n − 2 × LCS: the kept letters stay once in each word, so their count comes off both lengths.
Two strings, one allowed edit, and a question about making them match. When the only edit is deleting, the letters that survive form a common subsequence, so the problem is a longest common subsequence in disguise.
Approach
Before reading on, find the longest string you can get from both sea and eat by crossing out letters, then count how many letters you crossed out in total. Aim for O(m · n) time.
Let each cell stand for two prefixes
dp[i][j] is the LCS length of the first i letters of word1 and the first j letters of word2. Row 0 and column 0 stay 0, because an empty prefix shares nothing. That extra row and column let every cell read its neighbours without bounds checks.
When the last letters match, keep them
If word1[i - 1] == word2[j - 1], set dp[i][j] = dp[i - 1][j - 1] + 1. Pairing these two letters never hurts: a common subsequence that leaves one of them out can use this pair for its last letter instead and stay just as long.
When they differ, drop one of them
Two different letters cannot both end the common subsequence, so at least one of them is not in it. Drop either one and keep the better result: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]).
Turn the LCS into a deletion count
Fill the table row by row, so the cells above, to the left and up-left are always ready. dp[m][n] is the LCS length L. word1 loses its other m − L letters and word2 its other n − L, so return m + n - 2 * L.
Code in Python, C++ and Java
Common pitfalls
Subtracting the LCS only once
return m + n - dp[m][n]
return m + n - 2 * dp[m][n]
The kept letters stay in both words, so they come off both lengths. On sea and eat the LCS is ea, length 2: the wrong line returns 4, but deleting s and t is enough, so the answer is 2.
Reading the words at the table index
if word1[i] == word2[j]:
if word1[i - 1] == word2[j - 1]:
Row i stands for the first i letters, so its last letter is word1[i - 1]. Reading word1[i] compares the wrong pair of letters, and at i = m it runs past the end of the word: on sea and eat it raises an IndexError.
Edge cases
ab and cdNo pair ever matches, so every cell stays 0 and the LCS is 0. Every letter has to go, and the answer is 2 + 2 = 4.
abc and acThe LCS is the whole shorter word, so only the longer word loses letters: 3 + 2 − 2 × 2 = 1, deleting the b.
Complexity
Delete Operation for Two Strings FAQ
How is this different from Edit Distance?
Edit Distance also lets you insert and replace a letter, so its table takes a minimum over three moves, one of them a diagonal replace. Here only deleting is allowed, so there is no replace move. On a and b, edit distance is 1 (replace the letter), while this problem needs 2 deletions.
Is it the same table as Longest Common Subsequence?
Yes. Longest Common Subsequence fills exactly this table and returns dp[m][n]; only the last line differs. You can also count deletions directly: set row 0 to j and column 0 to i, copy the up-left cell on a match, and take 1 + min(up, left) otherwise. Both give the same answer.
How do you see which letters get deleted?
Walk back from dp[m][n]. Where the two letters match, keep that letter and step up-left; otherwise step to whichever of up or left holds the larger number. The letters you never kept are the deletions. For sea and eat the walk keeps e and a, so s goes from the first word and t from the second.