Skip to content
Onsite

Edit Distance

MediumP1Coding

Given two strings word1 and word2, return the minimum number of operations needed to turn word1 into word2. In one operation you may insert a character, delete a character, or replace a character with another.

Example 1

Input
word1 = "horse", word2 = "ros"
Output
3
Why
horse -> rorse (replace h with r) -> rose (delete r) -> ros (delete e).

Example 2

Input
word1 = "intention", word2 = "execution"
Output
5

Example 3

Input
word1 = "", word2 = "abc"
Output
3

Constraints

  • 0 <= word1.length, word2.length <= 500
  • Strings contain lower-case English letters.

Hints

Run checks the 3 visible tests, Submit all 11.