Lesson
The theory — Levenshtein Distance Visualizer
The Levenshtein distance between two strings is the smallest number of single-character edits that turns one into the other, where an edit is an insertion, a deletion, or a substitution. It is a minimum over every possible way of doing the job, which is what makes it hard to compute by eye and easy to compute with a grid: there are astronomically many edit sequences, and this counts the shortest without listing any of them.
What each symbol means
m, n- the lengths of the two strings, A and B.
D(i, j)- the cheapest way to turn the first i characters of A into the first j characters of B. One cell of the table on this page.
[aᵢ ≠ bⱼ]- costs 1 when those two characters differ and 0 when they match — the substitution price, which is free if there is nothing to substitute.
d- the answer,
D(m, n): the bottom-right cell.
Where the formula comes from
- Decide what one cell means before filling any of them. Let
D(i, j)be the fewest edits that turn A’s firsticharacters into B’s firstj. The number you want isD(m, n), and every other cell is a smaller version of the same question. - The edges need no thinking. To build B’s first
jcharacters starting from nothing you must insert alljof them, soD(0, j) = j. To reduce A’s firsticharacters to nothing you must delete alli, soD(i, 0) = i. That is the top row and the left column of the grid, and it is why they simply count upwards. - Now the step that does all the work. Take any optimal script for
D(i, j)and look only at its last move. There are three possibilities and no others: it deletedaᵢ, leaving the jobD(i−1, j); it insertedbⱼ, leavingD(i, j−1); or it pairedaᵢwithbⱼ, leavingD(i−1, j−1). Each of those is a cell you have already filled. - So the cell is the cheapest of the three:
D(i, j) = min( D(i−1, j) + 1, D(i, j−1) + 1, D(i−1, j−1) + [aᵢ ≠ bⱼ] ). Fill the grid left to right and top to bottom, and when you reach any cell its three neighbours are already known. That is the whole algorithm —m × ncells, three comparisons each.
How to read what you see
The grid on this page is that table. Rows are A, columns are B, and the extra row and column at the top-left are the empty prefixes from step 2 — which is why they count 0, 1, 2, 3. Cell shading is the value: the pale diagonal corridor is where the two strings still agree cheaply. Hover or tap any cell and the panel underneath spells out step 3 for that cell — the three candidates, each with its arithmetic, and the winner highlighted. The coloured line is one optimal route traced back from the bottom-right corner, and the alignment strip below it is that same route written out as characters.
- Assumes
- Every edit costs exactly 1, and characters are compared for exact equality — no case folding, no Unicode normalisation, and no notion of which characters look or sound alike. Two consequences are visible here. What gets counted here is UTF-16 code units, not what you would call letters: type a single emoji into one box and the table is 3 × 1, because one emoji is two units, and deleting it costs 2. And
cafétyped with a precomposedéagainstcafétyped aseplus a combining accent reads distance 2 while the two look identical on screen. - Breaks when
- The grid is
m × ncells, so the work grows as the product of the lengths. That is fine for two words and hopeless for a query against a million-entry dictionary, which is a million grids. The natural response is to look for a cleverer algorithm, and the surprise is that essentially none exists: Backurs and Indyk showed in 2015 that a strongly subquadratic edit distance would refute the Strong Exponential Time Hypothesis. Real spell-checkers do not beat the bound, they avoid it — cutting off once the distance exceeds a small limit, or indexing so that almost every candidate is never compared at all.
Problem solved in full
-
The cheapest edit sequence to turn kitten into sitting 5 steps
Turn kitten into sitting. Find the cheapest edit sequence and prove no cheaper one exists — then work out what this costs when the second word is a whole dictionary.
-
The recurrence is the whole algorithm. Each cell asks what it costs to arrive here by deleting, by inserting, or by lining the two current characters up — the last option free when they agree. A cell needs only the three neighbours above and to its left, so one left-to-right sweep fills the table.
-
Before filling anything, bracket the answer. The lengths differ by one, so at least one insertion is unavoidable; and rewriting the word outright costs 7. The answer lies between, which already disposes of most guesses.
-
Three edits do it: substitute k → s, substitute e → i, insert g. Each is a single legal edit and the chain ends on the target.
-
Exhibiting three edits only proves the distance is at most 3. The table proves it is exactly 3, because every route to the bottom-right corner is scored and the minimum is taken at every cell along the way. That is the difference between finding an answer and knowing it cannot be beaten.
-
The cost is one cell per pair of characters — 42 here, which is nothing. Against a 100 000-word dictionary at seven letters apiece it is over four million cell updates for a single lookup.
Answer
The tool prints 3 and a similarity of 57.14%, which is 1 − 3/7: the distance normalised by the longer word. Both come from a table you can fill by hand in a minute. What does not scale is the table — the work is Θ(mn), so doubling both strings quadruples it, and every spellchecker you have used is built around not doing this. The escape is not a faster table. It is that Levenshtein satisfies the triangle inequality, so d(a, c) ≤ d(a, b) + d(b, c) lets an index prove a candidate is too far away without ever measuring how far.
-
References (4)
- The theory section ends on why nobody has a much faster algorithm. This is the result that says they probably cannot have one: A. Backurs & P. Indyk, "Edit Distance Cannot Be Computed in Strongly Subquadratic Time (Unless SETH is False)." SIAM Journal on Computing 47(3), 1087–1097, 2018; first presented at STOC 2015.
- The grid this tool fills, and why filling each cell once is enough: R. A. Wagner & M. J. Fischer, "The String-to-String Correction Problem." Journal of the ACM 21(1), 168–173, 1974 — the dynamic-programming algorithm for the edit distance Levenshtein had defined in 1965.
- The fourth move in the Damerau cost model, and the survey of typing errors that argued for it: F. J. Damerau, "A technique for computer detection and correction of spelling errors." Communications of the ACM 7(3), 171–176, 1964.
- Insight block 3 ends on the problem of recovering the route without keeping the grid. This is the paper that solved it: D. S. Hirschberg, "A linear space algorithm for computing maximal common subsequences." Communications of the ACM 18(6), 341–343, 1975.