Levenshtein Distance

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write editDistance(a, b): return the fewest single-character edits that turn the string a into the string b, where an edit inserts a character, deletes one or replaces one with another. This is the Levenshtein distance. Either string may be empty. For example, editDistance("horse", "ros") is 3: replace h with r, delete the second r, then delete e. The checks include two strings of 2,000 characters, so trying every sequence of edits is too slow: work out the distance between every pair of beginnings of a and b.

Your code must pass

  • Finds the fewest edits
  • Counts every character when one string is empty
  • Returns 0 for the same string
  • Is fast enough for two strings of 2,000 characters