Edit Distance
Problem statement
Given two strings word1 and word2, return the minimum number of single-character operations needed to transform word1 into word2.
Each operation is one of the following:
- Insert one character.
- Delete one character.
- Replace one character.
Function
minDistance(word1: String, word2: String) → intExamples
Example 1
word1 = "horse"word2 = "ros"return = 3Replace h with r, delete the second r, and delete e.
Example 2
word1 = "intention"word2 = "execution"return = 5A minimum transformation uses five insert, delete, or replace operations.
Example 3
word1 = ""word2 = "abc"return = 3Insert a, b, and c.
Constraints
0 <= word1.length <= 500.0 <= word2.length <= 500.- Both strings contain only lowercase English letters.