Problem description

You are given two strings, word1 and word2. In a single operation you may insert one character, delete one character, or replace one character.

Return the minimum number of operations required to transform word1 into word2.

Example 1

Input: word1 = "horse", word2 = "ros"

Output: 3

Replace 'h' with 'r' to get rorse, delete 'r' to get rose, then delete 'e' to get ros. Three operations is the minimum.

Example 2

Input: word1 = "intention", word2 = "execution"

Output: 5

A minimal sequence uses one replace and several inserts and deletes to align the two words in five total edits.

Constraints

  • 0 <= word1.length, word2.length <= 500
  • word1 and word2 consist of lowercase English letters.

After the pattern

Now solve one you haven't seen.

Command Correction Cost is our own problem on the technique behind Edit Distance: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free