Skip to content
HardDpAI interview only

Edit Distance

Asked atgoogleamazonmetamicrosoftbloomberglinkedinuber

01 · Problem

Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.

You have the following three operations permitted on a word:

  • Insert a character
  • Delete a character
  • Replace a character

02 · Examples

Example 01
Input
word1 = "horse", word2 = "ros"
Output
3

horse -> rorse (replace 'h' with 'r') -> rose (remove second 'r') -> ros (remove 'e'). Three operations total.

Example 02
Input
word1 = "intention", word2 = "execution"
Output
5

Five operations: remove 't', replace 'i' with 'e', replace first 'n' with 'x', replace second 'n' with 'c', insert 'u'.

03 · Constraints

  • 010 <= word1.length, word2.length <= 500
  • 02word1 and word2 consist of lowercase English letters

04 · Optimal complexity

Time
O(m * n)
Space
O(m * n)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.