Edit Distance
Given two strings word1 and word2, return the minimum number of single-character operations required to convert word1 into word2. The allowed operations are insert a character, delete a character, and replace a character.
Open official problem prompt ↗We want the cheapest sequence of single-character edits that rewrites word1 as word2.
Like a spell-checker measuring how far a typo is from a dictionary word by counting the fewest keystroke fixes.
- Input
- word1 = "horse", word2 = "ros"
- Output
- 3
- Why
- horse -> rorse (replace h->r) -> rose (delete r) -> ros (delete e) uses three operations, and none is shorter.
0 <= word1.length, word2.length <= 500word1 and word2 consist of lowercase English letters.