3.7.12 · D3Algorithm Paradigms

Worked examples — DP problems — edit distance (Levenshtein)

3,529 words16 min readBack to topic

This page hunts down every kind of input edit distance can throw at you and works each one from the ground up. If the parent note taught you the machine, this page is the stress test — we feed it empty strings, identical strings, single characters, strings where every letter is wrong, a real-world spell-checker, and an exam twist, so you never hit a case you haven't already seen filled in.

First, three symbols must be earned before anything else uses them: the two strings and , and the distance function .

Now the rule that drives everything (built fully in the parent note):

Here "first letters" means: chop the string after position . For cat, the "first 2 letters" are ca. The letter we are currently deciding about is — the last one in that chop. That is the single most common bug, so we will name the current letters out loud in every example.


The scenario matrix

Every edit-distance problem falls into one of these case classes. Our job below is to fill every row with a fully worked example. (Recall , from the definitions above.)

# Case class What makes it special Covered by
C1 Both empty Degenerate: nothing to do Example 1
C2 One empty, one non-empty Pure base case (all inserts or all deletes) Example 1
C3 Identical strings Every char matches — diagonal carries 0 Example 2
C4 Single-char, equal vs unequal Smallest non-empty case, both branches Example 3
C5 Equal length, some substitutions Distance from swaps only, no length change Example 4
C6 Different length, mixed ops Insert/delete and substitute together Example 5
C7 Completely disjoint alphabets Worst case — distance Example 6
C8 Prefix/suffix contained One string sits inside the other Example 7
C9 Real-world word problem Spell-checker suggestion cost Example 8
C10 Exam twist: multiple optimal paths Same distance, different edit scripts Example 9

We also lean on a sanity bound used throughout: the distance is always between (the length gap — a lower bound, because you cannot avoid closing the gap) and (the length of the longer string — an upper bound, because worst case you fix one letter of it per op). In symbols:

We will re-check this band in every example below — it is the fastest way to catch an arithmetic slip.

The figure below plots this band on a number line, with the real distance (red dot) trapped inside it for the sundaysaturday example we solve in Example 5.

Figure — DP problems — edit distance (Levenshtein)
Figure s01 — a horizontal number line from to . A black double-headed arrow spans the allowed band from the lower bound to the upper bound . The red dot marks the actual answer , sitting inside the band. Read it as: no legal distance can fall left of or right of . We return to this exact picture in Example 5.


Example 1 — Empty strings (C1 + C2)

Forecast: Before reading on, guess all three. Empty→empty feels like… nothing. Empty→3-letter feels like… 3 builds. 3-letter→empty feels like… 3 removals.

Steps.

  1. (using with both lengths ). Why this step? Two empty strings are already identical; zero moves. This is the seed every table grows from.
  2. . The base-case rule says , so . Why this step? From nothing you can only insert. To produce c, then a, then t, you insert three times — there is no cheaper move because deletes and substitutions need an existing letter to act on.
  3. . The base-case rule says , so . Why this step? To reach the empty string you can only delete — remove d, o, g, three deletes.

Verify: For the middle case, , . Lower bound ; upper bound . The answer is pinched exactly between them, so it must be right. ✅ (The dog"" case is the mirror image, .)


Example 2 — Identical strings (C3)

Forecast: Same word, both sides. Guess the number before continuing.

Steps.

  1. apple, so and — equal lengths. Fill the diagonal. At : current letters a, a — they match, so . Why this step? A match copies the diagonal with no +1 (the "match" branch). Zero cost carries through.
  2. Each subsequent diagonal cell matches (p=p, p=p, l=l, e=e), so . Why this step? Every current-letter pair is equal, so each step re-copies down the diagonal. Look at the straight red diagonal of s in the figure below.
  3. Final answer .

Figure — DP problems — edit distance (Levenshtein)
Figure s02 — the full DP table for appleapple. Columns are headed by B=apple, rows by A=apple. The base row and column count up in black. The red diagonal of s runs from top-left to bottom-right; the red arrow points at the answer . Every matched letter copies the cell up-and-left, so zeros ride straight down.

Verify: Distance means "already equal" — and the strings are identical, so no edits are possible or needed. Sanity band: . The answer sits on the lower bound, exactly where identical strings should. ✅


Example 3 — Single characters, both branches (C4)

Forecast: One is a match, one is a mismatch. One should cost , the other should cost — which is which?

Steps for "a""a".

  1. Table is . Bases: , , . Why this step? Row 0 and column 0 must exist before we touch — that is the "forgot the base" mistake from the parent note.
  2. : current letters a, a match. So . Why this step? Match branch, pure diagonal, no +1.

Steps for "a""b".

  1. : a, b differ. So . Why this step? The cheapest predecessor is the diagonal , meaning we substitute a→b for one coin (Levenshtein's substitute operation — not a transposition; there is no "swap two letters" move in Levenshtein). Deleting a then inserting b would cost — the min correctly rejects it.

Verify: "a""a" gives ; sanity band . ✅ "a""b" gives ; band , and since the whole cost is one substitution, never a length change. ✅


Example 4 — Equal length, substitutions only (C5)

Forecast: Both strings in each pair have equal length. Guess: does equal length force the answer to be pure substitutions?

Steps for "cat""cot". Here .

  1. : c=c match. Why this step? Match branch carries forward.
  2. : current letters a, o differ. Why this step? The min picks the diagonal predecessor , i.e. substitute a→o. The delete path () and insert path () both cost , so each — the recurrence rejects them because substitution here is strictly cheaper. This is the "why substitute, not delete+insert" the formula settles automatically.
  3. : t=t match. So .

Steps for "abcd""wxyz". Here .

  1. Every diagonal cell has differing letters (a≠w, b≠x, c≠y, d≠z). At each, the diagonal predecessor beats the delete/insert neighbours by exactly the same -vs- margin as Step 2, so each contributes one substitution: , , , . Why this step? When lengths are equal, the min-over-three at every diagonal cell always favours substitution (cost ) over a delete+insert pair (cost ). Hence substitutions total.

Verify: "cat""cot" gives ; sanity band . ✅ "abcd""wxyz" gives ; band — it sits on the upper bound because every letter needed work. ✅


Example 5 — Different length, mixed operations (C6)

Forecast: These share s…day. Guess how many of the target letters need real work.

Steps.

  1. Fill the DP table row by row (rows = sunday, columns = saturday). The table below is computed by the recurrence; trace the red back-pointer path from the bottom-right answer up to the top-left corner.

Figure — DP problems — edit distance (Levenshtein)
Figure s03 — the DP table for sundaysaturday. Rows headed by A=sunday, columns by B=saturday. The bottom-right cell holds the answer . The red staircase traces one optimal back-pointer path from to : diagonal moves on matched letters (free) and the three costly moves — two inserts and one substitute.

  1. The shared letters s…u…d-a-y line up on the diagonal for free — these are the diagonal steps of the red path where letters match. What is left: saturday has an extra a and t that sunday lacks → insert 2 (two leftward steps in the path). And sunday's n must become rsubstitute 1 (one diagonal step where letters differ). Why this step? Matches are free, so the free diagonal run traces out a set of positions that agree in order between the two strings. This is where the LCS connection is precise: the free (match) diagonal steps form one common subsequence — but plain Levenshtein does not maximise it. Two related but different DPs: LCS counts matches to maximise; Levenshtein counts edits to minimise, and only when substitution and insert/delete all cost do they satisfy the identity for the substitution-free (LCS) edit model. We do not rely on that identity here; we just observe that longer shared runs shrink the distance.
  2. Total .

Verify: Back to Figure s01, whose band was built for this example: lower bound ; our answer (the extra is the forced substitution), and . The red dot in s01 sits at exactly , inside the band. ✅


Example 6 — Disjoint alphabets, worst case (C7)

Forecast: No letters in common, and lengths vs . Guess whether the answer is .

Steps.

  1. Since nothing matches, every cell falls into the "differ" branch — no free diagonals exist. The table below shows the whole grid; the answer is the bottom-right cell.

Figure — DP problems — edit distance (Levenshtein)
Figure s04 — the DP table for abcxyzw with fully disjoint letters. Rows headed A=abc, columns B=xyzw. Because no letters match, values only ever grow; the red bottom-right cell shows the answer , equal to . The red path is a mix of three substitutions and one insert.

  1. Reading the path: substitute a→x, b→y, c→z (three substitutions), then insert w — total . Why this step? When the alphabets are disjoint, no free matches exist, so the distance hits its ceiling: the longer string's length. You spend one op per letter of the longer string.

Verify: Sanity band . ✅ The disjoint case saturates the upper bound exactly — no input of these lengths can cost more than .


Example 7 — One string is a substring of the other (C8)

Forecast: In the first, is cat hiding inside cart? In the second, is cat a chunk of scatter? Guess each distance from the "extra letters" alone.

Steps for "cart""cat". Here , .

  1. The DP table below (top panel) shows the alignment. Follow the red path: c matches c, a matches a (diagonal, free), then cart still has r before its t while cat goes straight to t — the r is a leftover → delete it (one upward step).

Figure — DP problems — edit distance (Levenshtein)
Figure s05 — two stacked DP tables, each with its axes labelled. Top: table for cartcat; the left axis is labelled A=cart (rows), the top axis B=cat (cols), and the base row/column () are annotated. The red path shows two free diagonal matches then one upward delete step, answer . Bottom: table for catscatter; left axis A=cat (rows), top axis B=scatter (cols), base row/column annotated. The red path shows a free diagonal run through cat bracketed by leftward insert steps for s and ter, answer .

  1. So one extra letter r → distance .

Steps for "cat""scatter". Here , .

  1. scatter = s + cat + ter. We must insert s at the front and t,e,r at the back → inserts. All of cat matches for free in the middle (the free diagonal run in the bottom table). Why this step? When one string contains the other's letters in order plus extras, each extra is a single delete/insert.

Verify: "cart""cat": sanity band — the answer sits on the lower bound because only deletions were needed. ✅ "cat""scatter": band — again on the lower bound, pure insertions, no substitution since cat embeds cleanly. ✅


Example 8 — Real-world word problem: spell-checker (C9)

Forecast: Both candidates are letters, same as the typo. Guess which correction is "closer".

Steps.

  1. Both candidate distances come from filling a DP table; the figure shows the two answer cells side by side so you can see which is smaller.

Figure — DP problems — edit distance (Levenshtein)
Figure s06 — two DP tables, one per candidate. Left: recievereceive, its bottom-right answer cell in red reads . Right: recieverelieve, its red answer cell reads . The smaller red number wins the ranking.

  1. "recieve""receive": equal lengths (), so align letter-for-letter. r-e-c match, then the typo has i-e where the correct word has e-i — reversed, so two substitutions (i→e and e→i), then v-e match. Distance . Why this step? Equal lengths → align position-by-position; two positions differ → two substitutions.
  2. "recieve""relieve": r-e match, then c vs l differ (+1 substitute), i-e-v-e all match. Only one position differs → distance .
  3. Compare: receive costs , relieve costs . The checker ranks "relieve" first by raw edit distance. Why this step matters? Real spell-checkers add a dictionary-frequency tie-breaker precisely because raw distance can favour the "wrong" word — but on pure Levenshtein, relieve wins.

Verify: All three words have length . receive: band ; relieve: band . Both distances are pure-substitution counts ( and ) since . ✅


Example 9 — Exam twist: two optimal paths, same distance (C10)

Forecast: The letters are the same, just reordered. Levenshtein has no transposition operation (you cannot swap two letters in one move), so a reorder isn't free — guess the cost.

Steps.

  1. Build the table (). Bases: and . Then (ab, substitute), (a=a match, carries ), (b=b match, carries ), and .

Figure — DP problems — edit distance (Levenshtein)
Figure s07 — the DP table for abba, answer in the red bottom-right cell. Three red arrows fan out of that cell to all three predecessors (, , ), which all tie at . Because three back-pointers are equally optimal, more than one edit script achieves the minimum.

  1. At all three predecessors tie at , so the figure draws three red back-pointer arrows — that tie is exactly why multiple optimal edit scripts exist. Two natural scripts:
    • Substitute a→b, then substitute b→a.
    • Delete a, then insert a at the end (a "rotate" done as delete+insert). Both cost . Why this step? Each distinct back-pointer path from bottom-right to top-left decodes into a distinct edit script of the same total cost.

Verify: Levenshtein has no transposition operation, so swapping two adjacent letters costs , not . Sanity band — sits on the upper bound. ✅ (If transpositions were allowed — the Damerau–Levenshtein variant — the answer would be ; standard Levenshtein gives .)


Recall Quick self-test across the matrix

Cover the answers and reconstruct each. ::: ::: (three inserts) ::: (identical) ::: (one substitution) ::: (one substitution) ::: (four substitutions) ::: ::: (worst case ) ::: (delete r) ::: (four inserts) ::: ::: (no free transposition)


Connections

  • Parent: Edit Distance — the machine these examples stress-test.
  • Longest Common Subsequence — the free diagonal run relates to a shared subsequence (Example 5), though LCS maximises matches while Levenshtein minimises edits.
  • Sequence Alignment — spell-checking (Example 8) is small-scale alignment.
  • Optimal Substructure · Overlapping Subproblems — why the recurrence is correct on every case above.
  • Recursion and Memoization — the top-down way to compute any of these.
  • Space Optimization in DP — every table above needs only the previous row.
  • Dynamic Programming — the umbrella paradigm.