Algorithms

Edit Distance (Levenshtein)

Compare two strings and watch the Levenshtein matrix fill in. The final cell is the minimum number of edits needed to transform one string into the other.

8 minLevel 3/3The interactive stage stays ad-free.
Try:Case matters · max 10 characters
FromkittenTosittingDistance3Cell6, 7
∅sitting
∅01234567
k11234567
i22123456
t33212345
t44321234
e55432234
n66543323

This cell kitten → sitting: 3

Characters differ: choose the cheapest operation.

↖ replace/match 4↑ delete 5← insert 3

Distance: 3

k → se → iinsert g

A diagonal move is a match (cost 0) or a replace (cost 1). Horizontal and vertical moves are insert and delete. The highlighted path is one optimal edit sequence. Edit either word to turn this into a reusable tool; ties prefer the diagonal.

What you are seeing

1

Rows and columns are prefixes of the two strings. Each cell stores the edit distance between those prefixes.

2

If the current characters match, the diagonal value carries over. If not, take one plus the minimum of insert, delete, and replace.

3

The bottom-right cell is the answer. Backtracking reveals the actual edit sequence.

Why it matters

Edit distance powers spell checkers, DNA alignment, fuzzy search, and diff tools. It is also a clean example of 2D dynamic programming.

Complexity & details

Time: O(mn), space: O(mn) for the full matrix (O(min(m,n)) with rolling rows).

Frequently asked questions

What counts as one edit?+

Inserting one character, deleting one character, or replacing one character. Substituting a character is one replace operation.

Where is edit distance used?+

Spell correction, DNA sequence alignment, plagiarism detection, fuzzy matching, and version-control diffs all use variants of edit distance.