Skip to content
Lesson 1 of 10

Step 1 of 5 · Reading · ~1 min

Read

Edit Distance

Edit Distance

The Levenshtein distance between two strings is the minimum number of single-character insertions, deletions, or substitutions to transform one into the other.

"kitten" -> "sitting"  distance 3:
  kitten -> sitten    (substitute k -> s)
         -> sittin    (substitute e -> i)
         -> sitting   (insert g)

Standard dynamic programming:

python

Time: O(NM). Space: O(NM) but can be reduced to O(min(N,M)).

For diff specifically: we usually count line-level edits, not character-level. Diff is "shortest edit script" between sequences of lines.

Up nextLongest Common SubsequenceEdit Distance

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…