Edit Distance

Hard

Given word1 and word2, return the minimum number of single-character insert, delete or replace operations needed to turn word1 into word2.

Example 1

minDistance()
Input
word1 = "horse", word2 = "ros"
Output
3

Why

horse → rorse → rose → ros

Example 2

minDistance()
Input
word1 = "intention", word2 = "execution"
Output
5
Constraints
0 <= word1.length, word2.length <= 500
Both words are lowercase English letters.

Topics

Dynamic ProgrammingStrings

Implement minDistance in the editor and return the answer — printing it is never graded.