Skip to content
Decoder

Edit Distance

Hard

Problem

Return the minimum number of single-character edits needed to turn string a into string b. An edit is one of: insert a character, delete a character, or replace a character.

Example 1

Input
a = "horse", b = "ros"
Output
3
Why
horse → rorse → rose → ros

Example 2

Input
a = "kitten", b = "sitting"
Output
3

Example 3

Input
a = "", b = "abc"
Output
3
Why
insert all three

Constraints

  • 0 <= a.length, b.length <= 500
  • Both strings contain only lowercase letters.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.