AlgoFrames

Edit distance

Standard · Write code · about 20 min

Return the fewest operations that turn word1 into word2. One operation inserts a character, deletes a character, or replaces a character.

Example 1

Input:

["horse","ros"]

Output:

3

horse → rorse (replace h) → rose (delete r) → ros (delete e).

Example 2

Input:

["a","b"]

Output:

1

Replace a with b.

Constraints

  • 1 ≤ word1.length, word2.length ≤ 500
  • Both words use lowercase English letters.

Learn the idea first: String DP: LCS, edit distance, palindromic subsequence, a visual lesson.

More String DP: LCS, edit distance, palindromic subsequence problems