Skip to content
Edit Distance
step 1/5

Reading — step 1 of 5

Read

~1 min readEdit 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.

Discussion

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

Sign in to post a comment or reply.

Loading…