FastPrepEdit Distance

Edit Distance

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

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

One operation may insert one character, delete one character, or 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

Five insert, delete, or replace operations are sufficient and optimal.

Example 3

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

Insert the three characters of the target.

Constraints

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

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int minDistance(String word1, String word2) {
    // Write your code here.
}
word1"horse"
word2"ros"
expected3
Checking account…