Delete Operation for Two Strings - Problem

Given two strings word1 and word2, you need to find the minimum number of deletion operations required to make both strings identical.

In one operation, you can delete exactly one character from either string. Your goal is to transform both strings into the same final string using the fewest possible deletions.

Example: If word1 = "sea" and word2 = "eat", you can delete 's' from "sea" and 't' from "eat" to get "ea" in both cases, requiring 2 deletions total.

The key insight is that the optimal solution preserves the longest common subsequence between the two strings, as these characters don't need to be deleted from either string.

Input & Output

example_1.py โ€” Basic Case
$ Input: word1 = "sea", word2 = "eat"
โ€บ Output: 2
๐Ÿ’ก Note: Delete 's' from "sea" to get "ea", and delete 't' from "eat" to get "ea". Both strings become "ea" with 2 total deletions.
example_2.py โ€” Identical Strings
$ Input: word1 = "leetcode", word2 = "leetcode"
โ€บ Output: 0
๐Ÿ’ก Note: Both strings are already identical, so no deletions are needed.
example_3.py โ€” No Common Characters
$ Input: word1 = "abc", word2 = "xyz"
โ€บ Output: 6
๐Ÿ’ก Note: No characters match, so we must delete all 3 characters from "abc" and all 3 characters from "xyz" to get empty strings.

Visualization

Tap to expand
Dynamic Programming Solution ProcessDP Table Evolutioneat0123s1123e1122a2122Decision Process1. Characters match (e==e): Copy diagonal value2. Characters differ (sโ‰ e): min(left, top) + 13. Characters match (a==a): Copy diagonal value๐Ÿ”‘ Key Insight: Preserve longest common subsequence "ea"Total deletions = length(word1) + length(word2) - 2ร—LCS_lengthFor "sea" and "eat": 3 + 3 - 2ร—2 = 2 deletionsAlgorithm VisualizationStart: "sea" vs "eat"Build DP tablebottom-upFind LCS: "ea"Length = 2Answer: 2 deletions(s from sea, t from eat)
Understanding the Visualization
1
Initialize Base Cases
First row and column represent cost of making one string empty
2
Compare Characters
When characters match, copy diagonal value (no deletion needed)
3
Handle Mismatches
When characters differ, choose minimum cost path
4
Trace Solution
Bottom-right cell contains the minimum total deletions
Key Takeaway
๐ŸŽฏ Key Insight: Dynamic programming efficiently computes the longest common subsequence, which minimizes total deletions by maximizing preserved characters.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(mร—n)

Fill each cell in the mร—n table exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(mร—n)

2D DP table stores results for all subproblems

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค word1.length, word2.length โ‰ค 500
  • word1 and word2 consist of only lowercase English letters
  • The answer will always fit in a 32-bit integer
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
67.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen