Find the minimum number of deletions needed to make two strings equal.
Problem
You are given two strings word1 and word2. In one operation, you may delete exactly one character from either string.
Return the minimum number of deletions required so that the two strings become identical.
A string is considered identical when both strings have exactly the same sequence of characters after deletions.
Goal
Compute the smallest total number of deletions across both strings needed to make them equal.
Input Format
- Two strings
word1andword2. - Each string contains lowercase English letters.
Output Format
- Return a single integer: the minimum number of deletions needed to make the strings equal.
Constraints
0 <= word1.length, word2.length <= 500word1andword2consist of lowercase English letters only.
Example 1
Input
word1 = "sea", word2 = "eat"
Output
2
Explanation
Delete 's' from "sea" and 't' from "eat". Both strings become "ea".
Example 2
Input
word1 = "leetcode", word2 = "etco"
Output
4
Explanation
One optimal way is to keep the common subsequence "etco" and delete the other characters from "leetcode".
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.