Skip to main content
Back to problems
Leetcode
Medium
Dynamic Programming
DP on Strings
Strings
Google
Delete Operation for Two Strings

Find the minimum number of deletions needed to make two strings equal.

Acceptance 100%
Problem Statement

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 word1 and word2.
  • 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 <= 500
  • word1 and word2 consist of lowercase English letters only.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.