Edit Distance

Given two strings word1 and word2, return the minimum number of operations required to convert word1 into word2.

You may perform the following operations on word1:

  • Insert a character.
  • Delete a character.
  • Replace a character.
Example 1
Inputword1 = "horse", word2 = "ros"
Output3
Replace 'h' with 'r', delete the second 'r', and delete 'e' to transform "horse" into "ros" in 3 operations.
Example 2
Inputword1 = "intention", word2 = "execution"
Output5
Delete 'd', replace 'i' with 'e', insert 'c', replace 'u' with 'x', and insert 'n' to transform "intention" into "execution" in 5 operations.

Constraints

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

Asked at 31 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate