Levenshtein Edit Distance Visualizer — Free Dynamic Programming Table Demo

Levenshtein Edit Distance Dynamic Programming Visualizer · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

What's included

Features

Cell-by-cell filling
Row by row, in dependency order.
Recurrence written out
Delete, insert and keep/substitute costs.
Source highlighting
The three cells each value comes from.
Base cases explained
Row 0 and column 0.
Traceback path
The cheapest route highlighted in green.
Edit script
Keep, replace, insert and delete steps listed.
Any word pair
Rebuild with your own inputs.
Safe rendering
Input is escaped before display.

About this UI Snippet

Edit Distance — Dynamic Programming You Can Watch

Screenshot of the Levenshtein Edit Distance Dynamic Programming Visualizer snippet rendered live

Levenshtein edit distance is the minimum number of single-letter insertions, deletions and substitutions needed to turn one word into another: "kitten" to "sitting" takes three. It powers spell checkers, fuzzy search, DNA sequence comparison and diff tools. It's also the most approachable example of dynamic programming, because the whole computation is a table you can look at.

What a cell means

Cell (i, j) holds the edit distance between the first i letters of the first word and the first j letters of the second. The answer is the bottom-right cell. Row 0 and column 0 are base cases: turning a prefix into the empty string costs one deletion per letter.

The recurrence

Every other cell looks at three neighbours that are already filled:

- above, plus 1 — delete the first word's letter - left, plus 1 — insert the second word's letter - the diagonal, plus 0 if the letters match or 1 if they differ — keep or substitute

and takes the minimum. Stepping through, the three source cells are highlighted and each option's cost is written out, so you can check the arithmetic yourself.

Why this is dynamic programming

A naive recursive solution recomputes the same prefix pairs exponentially many times. The table computes each pair once, in an order where its dependencies already exist, giving O(m × n) time.

Recovering the edits

The number alone doesn't say *which* edits. Walking back from the corner, choosing at each step a neighbour that could have produced the current value, traces a cheapest path. Diagonal moves are keeps or replacements, upward moves are deletions and leftward moves are insertions. There can be several equally cheap paths; this one prefers keeps, then substitutions.

Try your own words

Enter any two words up to ten letters and rebuild the table.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to walk through one cell of kitten to sitting and explain why each candidate cost makes sense. Ask it to add the Damerau transposition rule, weighted costs (for example cheaper substitutions between keys that are close on a keyboard), a two-row memory-optimised version, or a mode that shows all equally cheap traceback paths. It can also turn this into an LCS (longest common subsequence) visualizer.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an interactive Levenshtein edit distance visualizer in plain HTML, CSS and JavaScript.

Requirements:
- Two text inputs (up to ten characters each, defaulting to "kitten" and "sitting") and a Build button.
- Render the dynamic programming table with the first word down the side and the second across the top, both prefixed with an empty-string row and column already filled with 0, 1, 2 and so on.
- A "Next cell" button that fills one cell at a time, row by row, highlighting the current cell and its three source cells, and showing the delete, insert and keep/substitute costs and the chosen minimum in words.
- A "Fill all" button that completes the table.
- When the table is complete, trace back from the bottom-right cell to highlight one cheapest path, and list the resulting edits as keep, replace, insert and delete steps, with the total distance.
- Escape user input before inserting it into HTML.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="lv">
  <div class="lv-top">
    <div>
      <h2>Edit distance, one cell at a time</h2>
      <p>Each cell = fewest edits to turn the first <i>i</i> letters of the top word into the first <i>j</i> letters of the side word.</p>
    </div>
    <form class="lv-form" id="lvForm">
      <label>From <input id="lvA" value="kitten" maxlength="10" autocomplete="off"></label>
      <label>To <input id="lvB" value="sitting" maxlength="10" autocomplete="off"></label>
      <button type="submit">Build table</button>
    </form>
  </div>
  <div class="lv-main">
    <div class="lv-tablewrap"><table class="lv-table" id="lvTable"></table></div>
    <aside class="lv-side">
      <div class="lv-ctrl">
        <button type="button" id="lvStep">Next cell</button>
        <button type="button" id="lvFill" class="ghost">Fill all</button>
      </div>
      <div class="lv-formula" id="lvFormula" aria-live="polite"></div>
      <ol class="lv-ops" id="lvOps"></ol>
    </aside>
  </div>
</div>

Step by step

How to Use

  1. 1
    Enter two wordsUp to ten letters each; press Build table.
  2. 2
    Step throughNext cell fills one cell and explains its three candidate costs.
  3. 3
    Watch the sourcesThe cells above, left and diagonal are highlighted.
  4. 4
    Fill allComplete the table at once.
  5. 5
    Read the editsThe green path and list show one cheapest edit sequence.

Real-world uses

Common Use Cases

Learning dynamic programming
The canonical first DP table.
Interview preparation
Edit distance is a classic question.
Search and autocomplete
Understand typo-tolerant matching.
Bioinformatics intros
Sequence alignment builds on this.
Teaching
Step through with students on a projector.
Related: Fuse.js Typo-Tolerant Search
Fuzzy matching in practice: Fuse.js Typo-Tolerant Product Search.
Related: Text Diff Checker
Line-level differences: Code Diff Viewer.

Got questions?

Frequently Asked Questions

The minimum number of single-character insertions, deletions and substitutions needed to change one string into another. For example, kitten to sitting has a distance of 3.

Row 0 and column 0 hold 0, 1, 2 and so on. Each other cell is the minimum of the cell above plus 1, the cell to the left plus 1, and the diagonal cell plus 0 if the letters match or 1 if they differ.

O(m × n) time and space for strings of length m and n. If you only need the distance and not the edits, you can keep just two rows, reducing space to O(min(m, n)).

Trace back from the bottom-right cell. At each cell, move to a neighbour whose value could have produced it: diagonal for keep or substitute, up for delete, left for insert. Reverse the moves to get the edit script.

Standard Levenshtein counts a swap of adjacent letters as two edits. The Damerau–Levenshtein variant adds a transposition operation that costs one.