
Edit Distance (medium)
Problem Statement
Given two strings word1 and word2, return the least number of edits needed to transform word1 to word2.
You can perform any one edit from below three in a single operation on Word:
Inserta characterDeletea characterReplacea character
Examples
-
Example 1:
- Input: word1 =
"cat", word2 ="cut" - Expected Output: 1
- Justification: Only one operation is needed, replacing 'a' with 'u' in "cat" to make it "cut".
- Input: word1 =
-
Example 2:
- Input: word1 =
"sun", word2 ="satur" - Expected Output: 3
- Justification: Three operations needed: 1) Insert 'a' after 's', 2) Insert 't' after 'a', and 3) Replace 'n' with 'r'.
- Input: word1 =
-
Example 3:
- Input: word1 =
"book", word2 ="back" - Expected Output: 2
- Justification: Two operations are required: 1) Replace 'o' with 'a', and 2) Replace the second 'o' with 'c'. This changes "book" to "back".
- Input: word1 =
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory