Specify the symbols and costs first
Insertion adds one symbol, deletion removes one and substitution replaces exactly one. Keeping a matching symbol costs zero. The classical unit-cost definition does not allow swapping adjacent symbols as one operation. Adding that operation gives a different distance definition, such as an appropriately specified Damerau-Levenshtein variant.[1]
A symbol here is a Unicode code point. Case and whitespace are significant. One displayed character can contain multiple code points: ä can be precomposed or stored as a plus a combining diaeresis. Explicitly enabling NFC normalization unifies those two representations before comparison. It neither folds case nor removes spaces. An emoji with modifiers can still contain multiple code points.[3]
Minimum over allowed edit sequences with unit cost per insertion, deletion or substitution. The result is a nonnegative integer.[1]
Build the complete distance from short prefixes
D(i,j) is the distance between the first i symbols of the source and the first j symbols of the target. An empty source requires j insertions; reaching an empty target requires i deletions. Each interior cell compares three possible final steps. A matrix stores the shortest costs instead of solving the same subproblems repeatedly.[2]
The boundaries represent empty prefixes; c decides substitution cost. Interior recurrence indices start at one.[2]
Delete from the source prefix, insert into the source, or substitute or keep the final symbol.[2]
For lengths m and n in code points, the answer is D(m,n) at the bottom right. The full matrix needs O(mn) work and storage for nonempty strings. Computing only the distance can use two rows and storage linear in the shorter length. This calculator retains a bounded full matrix to show a traceback. An optimal traceback need not be unique, but its minimum cost is.[2]
Two complete comparisons
Three edits from kitten to sitting
- Given
- Source kitten, target sitting. ASCII symbols, unit costs, no transposition operation.
- Find
- Find the distance and one minimum-cost sequence.
Replace k with s and e with i. The intermediate strings are sitten and sittin.
Append g. The prefix matrix gives three for the complete string lengths.
Result: Minimum distance is 3; the displayed sequence uses two substitutions and one insertion.
Check: The edit sequence establishes an upper bound of three. The matrix comparing all three predecessors confirms the minimum. Length difference one is only a lower bound and would not prove that the answer is three.
Why ab to ba does not cost one
- Given
- Source ab and target ba under the same classical unit-cost rule.
- Find
- Calculate the complete small matrix.
Initialize empty prefixes with 0, 1, 2. The first interior row has values 1 and 1.
The final cell compares three candidates, each costing two. For example, substitute both symbols.
Result: Levenshtein distance is 2. An adjacent transposition is not one allowed operation here.
Check: Different strings exclude cost zero. One substitution changes only one position; one insertion or deletion changes the length. None of these single edits can transform ab into ba.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Exercises on symbols, recurrence and interpretation
Calculate · Foundation
Compare the empty string with Plan. What is the distance in either direction?
Show hint
Each letter must be inserted or deleted.
Learning goal: Apply the boundary condition for an empty string.
Show solution and calculation
Distance is 4 in either direction.
Insert P, l, a, n in one direction and delete those four symbols in the other.
D(0,4)=D(4,0)=4; substitution alone cannot change the length.
Calculate · Application
An interior cell has upper neighbor 2, left neighbor 1 and diagonal neighbor 2. The compared symbols match. What value follows?
Show hint
Only the diagonal has no extra cost for a match.
Learning goal: Evaluate the local matrix recurrence.
Show solution and calculation
Candidates are 3, 2 and 2+0, whose minimum is 2.
Equal final symbols do not mean the full prefixes are identical; previous costs remain.
Interpret · Application
Haus and Maus differ by one substitution. Does that imply 75 percent identical meaning?
Show hint
The algorithm has no knowledge of word meanings.
Learning goal: Distinguish character distance from semantic meaning.
Show solution and calculation
No.
Distance is 1 because H is replaced with M.
An additionally chosen score 1−d/4 would be 0.75, but is only a specified length normalization rather than a semantic measurement.
Other normalizations or tokenizations are separate choices.
Error analysis · Application
Someone obtains zero for ABC and abc although no preprocessing was specified. What changed?
Show hint
Compare the three code points at corresponding positions.
Learning goal: Treat case as part of the input.
Transfer · Challenge
The left string is the precomposed code point ä; the right consists of a followed by a combining diaeresis. Compare the raw distance with the result after NFC on both sides.
Show hint
Raw lengths are one and two, with no matching symbol.
Learning goal: Place Unicode normalization before the distance comparison.
Show solution and calculation
Without normalization the distance is 2: one substitution and one insertion.
One edit cannot both fix the different existing symbol and produce the required length.
NFC makes both strings the same precomposed representation, giving distance 0.
The results refer to differently preprocessed inputs.
Check your understanding with the formula’s reference test.
8 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Sources for this explanation
Quanta · Updated November 1, 2026
- NIST Dictionary of Algorithms and Data Structures: Levenshtein distance, allowed operations
- MIT 6.006 Spring2020, Recitation16: edit-distance prefix recurrence and boundary conditions
- Unicode Standard Annex15: Unicode Normalization Forms, canonical composition
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Developed unit costs, prefix recurrence and Unicode scope through original examples and solved exercises.
Cite this reference page
Quanta. Levenshtein edit distance. Updated November 1, 2026. https://quanta-study.de/en/formula-sheet/levenshtein-editierdistanz
When citing a specific scientific statement, also cite the specialist source linked to that statement.