Design Gurus Logo
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:

  • Insert a character
  • Delete a character
  • Replace a 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".
  • 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'.
  • 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".

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