Practice · Dynamic programming (2D) · 4 of 4

Fewest Edits Between Two Words

You can change a word with three kinds of edit: insert one character anywhere, delete one character, or replace one character with a different one.

Given word1 and word2, return the fewest edits that turn word1 into word2.

Example 1

Input
word1 = "sunday", word2 = "saturday"
Output
3
Insert a and t after the s, then replace the n with r.

Example 2

Input
word1 = "", word2 = "abc"
Output
3
Insert all three letters.

Constraints

  • word1 and word2 each hold 0 to 500 lowercase letters.