FastPrepEdit Distance

Edit Distance

Amazon logoAmazonâ—Ź MediumNEW GRADONSITE INTERVIEW

Problem statement

Given two strings word1 and word2, return the minimum number of single-character operations needed to transform word1 into word2.

Each operation is one of the following:

  • Insert one character.
  • Delete one character.
  • Replace one character.

Function

minDistance(word1: String, word2: String) → int

Examples

Example 1

word1 = "horse"word2 = "ros"return = 3

Replace h with r, delete the second r, and delete e.

Example 2

word1 = "intention"word2 = "execution"return = 5

A minimum transformation uses five insert, delete, or replace operations.

Example 3

word1 = ""word2 = "abc"return = 3

Insert a, b, and c.

Constraints

  • 0 <= word1.length <= 500.
  • 0 <= word2.length <= 500.
  • Both strings contain only lowercase English letters.

More Amazon problems

See Amazon hiring insights
Saved
public int minDistance(String word1, String word2) {
    // Write your code here.
}
Checking account…