Published by Dimitris Kougioumtzis · Updated
Find the minimum insertions, deletions, and substitutions needed to transform one string into another.
Prefix cells combine match, substitution, insertion, and deletion choices from already solved neighboring prefixes.
When to use Levenshtein edit distance
Approximate text matching and alignment recurrence study.
Choosing an alternative
LCS uses a different objective and operation model.
Worked example: input and state changes
Example input
kitten | sitting
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Base row and column: distance from an empty string.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0 ], "active": [], "kind": "matrix", "columns": 8, "rowLabels": [ "∅", "k", "i", "t", "t", "e", "n" ], "columnLabels": [ "∅", "s", "i", "t", "t", "i", "n", "g" ] }Step 11: Compare i and t; distance for prefixes 2, 4 = 3.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 1, 1, 2, 3, 4, 5, 6, 7, 2, 2, 1, 2, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0 ], "active": [ 20 ], "kind": "matrix", "columns": 8, "rowLabels": [ "∅", "k", "i", "t", "t", "e", "n" ], "columnLabels": [ "∅", "s", "i", "t", "t", "i", "n", "g" ] }Step 22: Compare t and s; distance for prefixes 4, 1 = 4.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 1, 1, 2, 3, 4, 5, 6, 7, 2, 2, 1, 2, 3, 4, 5, 6, 3, 3, 2, 1, 2, 3, 4, 5, 4, 4, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0 ], "active": [ 33 ], "kind": "matrix", "columns": 8, "rowLabels": [ "∅", "k", "i", "t", "t", "e", "n" ], "columnLabels": [ "∅", "s", "i", "t", "t", "i", "n", "g" ] }Step 32: Compare e and t; distance for prefixes 5, 4 = 2.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 1, 1, 2, 3, 4, 5, 6, 7, 2, 2, 1, 2, 3, 4, 5, 6, 3, 3, 2, 1, 2, 3, 4, 5, 4, 4, 3, 2, 1, 2, 3, 4, 5, 5, 4, 3, 2, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0 ], "active": [ 44 ], "kind": "matrix", "columns": 8, "rowLabels": [ "∅", "k", "i", "t", "t", "e", "n" ], "columnLabels": [ "∅", "s", "i", "t", "t", "i", "n", "g" ] }Step 43: Levenshtein distance = 3.
State at this checkpoint
{ "values": [ 0, 1, 2, 3, 4, 5, 6, 7, 1, 1, 2, 3, 4, 5, 6, 7, 2, 2, 1, 2, 3, 4, 5, 6, 3, 3, 2, 1, 2, 3, 4, 5, 4, 4, 3, 2, 1, 2, 3, 4, 5, 5, 4, 3, 2, 2, 3, 4, 6, 6, 5, 4, 3, 3, 2, 3 ], "active": [], "kind": "matrix", "columns": 8, "rowLabels": [ "∅", "k", "i", "t", "t", "e", "n" ], "columnLabels": [ "∅", "s", "i", "t", "t", "i", "n", "g" ] }
Expected final state
Levenshtein distance = 3.
Complexity and input bounds
Family: Dynamic programming. Time complexity: O(nm).
Space complexity: O(nm) displayed prefix table.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
First string | second string. Up to 16 letters each; either string may be empty.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Initialize empty-prefix boundaries; this Levenshtein variant does not treat transposition as one edit.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Strings are character-code arrays of lengths n and m (Python also accepts strings). Each length ≤ 16; empty strings are allowed. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(first, n, second, m) { let previous = Array(256).fill(0); let current = Array(256).fill(0); for (let j = 0; j <= m; j++) { previous[j] = j; } for (let i = 1; i <= n; i++) { current[0] = i; for (let j = 1; j <= m; j++) { let cost = 1; if (first[i - 1] == second[j - 1]) { cost = 0; } current[j] = previous[j - 1] + cost; if (previous[j] + 1 < current[j]) { current[j] = previous[j] + 1; } if (current[j - 1] + 1 < current[j]) { current[j] = current[j - 1] + 1; } } for (let j = 0; j <= m; j++) { previous[j] = current[j]; } } return previous[m]; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- The String-to-String Correction Problem
Robert A. Wagner; Michael J. Fischer (1974). Research paper.