Delete Operation for Two Strings - Problem
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.
The goal is to find the minimum number of deletions needed so that both strings become identical.
Input & Output
Example 1 — Basic Case
$
Input:
word1 = "sea", word2 = "eat"
›
Output:
2
💡 Note:
Delete 's' from word1 and 't' from word2 to make both strings "ea". Total: 2 deletions.
Example 2 — No Common Characters
$
Input:
word1 = "abc", word2 = "def"
›
Output:
6
💡 Note:
No common characters, so delete all 3 characters from word1 and all 3 from word2. Total: 6 deletions.
Example 3 — Identical Strings
$
Input:
word1 = "hello", word2 = "hello"
›
Output:
0
💡 Note:
Strings are already identical, no deletions needed.
Constraints
- 1 ≤ word1.length, word2.length ≤ 500
- word1 and word2 consist of only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Strings
Two strings that need to become identical
2
Find Common Parts
Identify longest common subsequence to keep
3
Calculate Deletions
Delete all characters not in common subsequence
Key Takeaway
🎯 Key Insight: Find the longest common subsequence and delete everything else - this gives the minimum number of deletions needed.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code