3.7.10 · D1Algorithm Paradigms

Foundations — DP problems — Longest Common Subsequence (LCS)

2,213 words10 min readBack to topic

This page assumes you have seen nothing. We build every letter, arrow, and bracket the parent LCS note leans on, one brick at a time. If a term below already feels obvious, skim — but each one is a place beginners silently trip.


1. What a "string" is

The picture: think of beads on a straight wire. Each bead is a character, and the wire fixes their order — you cannot slide bead 4 in front of bead 2 without cutting the wire.

Figure — DP problems — Longest Common Subsequence (LCS)

Why the topic needs it. LCS compares two such wires and asks which beads line up in both, keeping the wire order intact. Without a fixed order there would be no "same relative order" to protect.


2. Naming the strings and their characters: , , ,

The parent writes and . Let's earn every piece.

The picture: two rows of beads stacked. A finger labelled points at one bead in the top row ; a finger labelled points at one bead in the bottom row .

Figure — DP problems — Longest Common Subsequence (LCS)

3. Subsequence vs substring — the picture that separates them

This is THE distinction the whole topic rides on.

The picture — same string, two different rules:

Figure — DP problems — Longest Common Subsequence (LCS)

From ABCDE:

  • ACE is a subsequence (keep A, skip B, keep C, skip D, keep E — order preserved, gaps allowed).
  • ACE is not a substring (A, C, E are not next to each other).
  • BCD is a substring (a solid slice).

Why the topic needs it. LCS = longest subsequence common to both strings. Confusing it with substring gives a completely different recurrence (see the parent's mistake box). Nail this now and half the traps vanish. The contiguous cousin lives in Longest Common Substring.


4. Prefix — the chunks we actually recurse on

The picture: a sliding cut. Put a knife after position ; everything to the left of the knife is the prefix . Slide the knife right and the prefix grows.


5. The label — a function of two pointers

Why "length" and not the string itself? Lengths are single numbers, trivial to compare and add. We compute all lengths first (fast), then walk backwards to recover an actual string only if we want one (the parent's traceback). Separating "how long" from "which letters" keeps the core simple.

The final answer we want is — the box with both dials turned all the way up, i.e. both full strings.


6. The tools inside the recurrence: , , and the two cases

The parent's formula uses two operations. Both deserve a plain-words picture.

Here means "not equal to". The symbol inside a case is a test ("are these two characters the same?"), not an assignment.


7. The base case and why the grid has an extra row/column

The picture: an empty wire has no beads; no bead can match against nothing. So every cell in row or column of the grid is — these are the walls we build outward from.


8. Reading the grid: up, left, diagonal

Every filled cell is decided by three neighbours already computed:

The picture: stand on a cell. The match rule reaches to the diagonal (both dials down). The mismatch rule reaches up and left and keeps the bigger. This directional habit — "match goes diagonal, miss takes the max" — is the entire mechanical skill.

Recall Which neighbour for which case?

Match uses which neighbour? ::: The diagonal (up-left), plus 1. Mismatch uses which neighbours? ::: Up and left, take the max.


Prerequisite map

String = ordered characters

Index x_i, y_j pointers into strings

Lengths m and n

Subsequence keeps order allows gaps

Prefix X first i chars

Function L i,j returns a length

max operation keep the bigger

Empty prefix means length 0

m+1 by n+1 DP grid

LCS recurrence match diagonal miss max

Answer at corner L m,n


Equipment checklist

Cover the right side and test yourself. If any answer is fuzzy, reread that section before tackling the parent's derivation.

What does mean, and why is it NOT multiplication?
The character at position inside string ; the subscript is an address/pointer, not a product.
What are and ?
The lengths (number of characters) of and respectively.
Difference between subsequence and substring?
Subsequence keeps order but allows gaps (skips); substring must be contiguous (no gaps).
What is the prefix ?
The starting chunk of , its first characters with nothing skipped.
What is ?
The empty prefix "" — zero characters.
In plain words, what is ?
The length of the longest common subsequence of the first characters of and the first characters of .
What kind of object is ?
A function (a two-dial lookup box) taking two numbers and returning one length.
What does do and why use it on a mismatch?
Returns the bigger of ; we try both "drop one character" options and keep the better one.
Why does a match add exactly and step back on BOTH dials?
One shared letter is found for the end; it uses one character from each string, so both prefixes shrink by one (the diagonal).
Why is the base case ?
An empty prefix has no characters, so nothing can be shared.
Why is the grid instead of ?
To hold the row/column of "empty prefix" zeros at index 0, giving every cell safe up/left/diagonal neighbours.
Which cell holds the final answer?
The bottom-right corner , using both full strings.