3.7.10 · D3Algorithm Paradigms

Worked examples — DP problems — Longest Common Subsequence (LCS)

2,637 words12 min readBack to topic

Before anything, one reminder of the machine we are running (from the parent). We write and , and is the LCS length of the first letters of and the first letters of .


The scenario matrix

Every LCS problem falls into one of these cells. The examples below are labelled with the cell they hit, and together they cover all of them.

# Cell (case class) What makes it special Example
C1 Typical overlap Both strings non-empty, partial match with gaps Ex. 1
C2 Zero input (empty string) One string is "" Ex. 2
C3 No common characters Disjoint alphabets → answer 0 Ex. 3
C4 Identical strings Upper bound: LCS = whole string Ex. 4
C5 One is a subsequence of the other LCS = the shorter one entirely Ex. 5
C6 Ties / multiple valid LCS Traceback branches, several answers Ex. 6
C7 Repeated characters trap Same letter appears many times — order still rules Ex. 7
C8 Real-world word problem Two playlists / DNA / diff Ex. 8
C9 Exam twist LCS vs substring on the same input Ex. 9
Recall Why these nine and not more?

The recurrence has exactly three branches (base, match, mismatch). "Every scenario" means exercising each branch under every degenerate condition: empty input hits only the base row (C2), disjoint input hits only mismatch (C3), identical input hits only match (C4). The rest are combinations. There is no fourth kind of behaviour to worry about. Any LCS input reduces to one of these ::: yes — base, all-match, all-miss, or a mixture; the matrix covers each.


Example 1 — Typical overlap (cell C1)


Example 2 — Zero input, an empty string (cell C2)


Example 3 — No common characters (cell C3)


Example 4 — Identical strings (cell C4)


Example 5 — One string is a subsequence of the other (cell C5)


Example 6 — Ties and multiple valid answers (cell C6)


Example 7 — Repeated characters trap (cell C7)


Example 8 — Real-world word problem (cell C8)


Example 9 — Exam twist: LCS vs Substring on the same input (cell C9)


Coverage recap

Recall Did we hit every cell?

C1 Ex.1 · C2 Ex.2 · C3 Ex.3 · C4 Ex.4 · C5 Ex.5 · C6 Ex.6 · C7 Ex.7 · C8 Ex.8 · C9 Ex.9. Every branch of the recurrence exercised under every degenerate condition. The tightest general bounds on any LCS length ::: — hit 0 in C3, hit in C4/C5/C7. Which cell shows LCS and substring diverging ::: C9 (Ex. 9): 4 vs 2.


Active recall

LCS of ABCBDAB and BDCAB?
length 4 (BCAB or BDAB).
LCS of any string with ""?
0 — the base-case border alone.
LCS of two strings with disjoint alphabets?
0 — no match branch ever fires.
LCS of a string with itself (length )?
— the whole string; upper bound .
LCS of AAAA and AA?
2 — bottleneck is the shorter supply of As.
Longest Common Substring of ABCBDAB and BDCAB?
2 (AB); substring resets to 0 on mismatch.
General bounds on LCS length?
.
Why do ties give multiple LCS but same length?
equal up/left neighbours are two optimal parents; both length-optimal, different witness strings.

Connections

  • Parent topic — the recurrence these examples exercise.
  • Dynamic Programming — the table-filling paradigm behind every example.
  • Longest Common Substring — the contiguous variant contrasted in Ex. 9.
  • Edit Distance — same grid, different cell rule; a natural next stress test.
  • Longest Increasing Subsequence — reduces to LCS; the token-LCS trick of Ex. 8 reappears.
  • Diff Algorithm — Ex. 8 is a line-level diff.
  • Sequence Alignment — the DNA generalisation of Ex. 8.