Delete Operation for Two Strings

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.

30:00

Delete Operation for Two Strings
medium
Free written solution
Topics
Companies

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.

Example 1:
Input: {"word1":"sea","word2":"eat"}
Output: 2
Constraints:
  • 1≤word1.length,word2.length≤5001 \leq \text{word1.length}, \text{word2.length} \leq 500

  • word1 and word2 consist of only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word1":"sea","word2":"eat"}

Build an (4) x (4) table: dp[i][j] = fewest deletions to make the first i letters of word1 equal the first j letters of word2.

word1

s
e
a

word2

e
a
t
∅
e
a
t
∅
s
e
a

↘ letters match: keep both

↓ delete the word1 letter

→ delete the word2 letter

Variables
VariableValue
m3
n3
DepthFunction Call
Stack empty
0/12