Edit Distance - aloalgo

Edit Distance

Hard

You are given two strings, word1 and word2.

Your task is to determine the minimum number of operations required to transform word1 into word2.

Note that:

  • You are only permitted to use the following operations on word1:
    • Insert a single character.
    • Delete a single character.
    • Replace a single character.

Return the minimum count of operations as an integer.

Example 1

Inputs
word1 = "cat"
word2 = "cut"
Output
1
Explanation:
  1. cat -> cut (replace 'a' with 'u')

Example 2

Inputs
word1 = "sunday"
word2 = "saturday"
Output
3
Explanation:
  1. Insert 'a' after 's'
  2. Insert 't' after 'sa'
  3. Replace 'n' with 'r'

Example 3

Inputs
word1 = "same"
word2 = "same"
Output
0
Explanation:

Both strings are already identical, no edits needed.

Example 4

Inputs
word1 = "computer"
word2 = "science"
Output
7
Explanation:

computer -> science

Replace or delete most letters — minimal common overlap ('e') gives distance of 8.

Loading...
Inputs
word1 = "cat"
word2 = "cut"
Output
1

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!