Longest Common Subsequence
Longest common subsequence (LCS) models the alignment problem behind a code diff: find the longest sequence that appears in two versions in the same order, while allowing insertions and deletions between matches. The shared sequence becomes the stable backbone; everything outside it is material one version added, removed, or moved around.
Think of two people recalling the same trip: their accounts include different details, yet several events still occur in the same order. LCS keeps the longest ordered sequence shared by both accounts, skipping extra details without rearranging the events it keeps.
How longest common subsequence works
Brute force enumerates every candidate directly. If string a has length m, it has 2^m subsequences, including the empty one; generating each candidate and checking whether it appears in b grows exponentially even on short strings. Dynamic programming avoids repeating the same prefix comparisons.
Let dp[i][j] mean the LCS length of a[:i] and b[:j]. The extra row and column represent empty prefixes, so every cell in row 0 or column 0 is 0.
Each remaining cell looks at the trailing characters of its two prefixes. If a[i - 1] equals b[j - 1], that shared character extends a solution that excludes both trailing positions: dp[i][j] = dp[i - 1][j - 1] + 1. The diagonal matters. Reading top or left and adding 1 could reuse one of the current positions and overcount.
If the characters differ, they contribute no new shared character. The algorithm drops one trailing position or the other and keeps the better result: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]).
A tie means both shortened prefix pairs preserve the same optimal length, so either direction leads to a valid LCS. This implementation prefers up to make playback and output deterministic. The rule chooses one optimal sequence; it does not make that sequence unique.
The lower-right cell gives the optimal length. To recover the letters, start there and trace backward. A character match moves diagonally and emits that character. A mismatch moves toward a neighbor that preserves the current length.
Why greedy matching returns a shorter answer
Earliest-match greedy scans a from left to right and matches each character to the earliest still-available copy in b. On the default input, its first decision already loses an optimal match.
For a = ABCBDAB and b = BDCABA, greedy sees a[0] = A and takes the earliest A in b, at b[3]. That commitment advances its cursor past b[0..2] = B, D, C. Those positions are gone for good, including the B and C that a longer answer needs. Greedy then takes B at b[4] and A at b[5], producing ABA, length 3.
Dynamic programming avoids that early commitment by preserving the alternatives represented by top and left prefix states. The finished table recovers BCBA, using B at b[0], C at b[2], B at b[4], and A at b[5], for length 4. Greedy's ABA is a valid subsequence; it is shorter because an early local match closed off a longer alignment.
Longest common subsequence step by step
The default run compares ABCBDAB with BDCABA.
-
The greedy prelude takes
Aata[0]and matches it tob[3]. The crossed-outB,D, andCshow the cost immediately: after this match, greedy cannot revisit them. -
Greedy continues from the right of
b[3], matchesBata[1]tob[4], then matchesAata[5]tob[5]. It stops withABA, length 3. The animation leaves this result visible as the baseline to beat. -
DP initializes row 0 and column 0 to zero. These cells answer every comparison involving an empty prefix.
-
The table fills left to right, one row at a time. The first three cells compare
AwithB,D, andC; both candidates are 0, so each cell writes 0. No traceback terminology is needed yet. Atdp[1][4],AmatchesA, so diagonal 0 becomes 1. Atdp[2][1],BmatchesBand also writes 1. -
Later cells compare competing prefix alternatives. A mismatch copies the larger of top and left. At
dp[3][5], both neighbors equal 2, so either direction preserves an optimal prefix. The lesson records up as its deterministic tie choice. -
When the final cell is filled,
dp[7][6] = 4lights up beside four empty result slots. The length is known, but the letters are not. That is the reason traceback exists. -
Traceback starts at
(7, 6).BandAdiffer, and the top neighbor preserves 4, so the cursor moves to(6, 6). ThereAmatchesA; the animation placesAin the rightmost result slot and moves diagonally to(5, 5). -
From
(5, 5), the cursor moves up to(4, 5), whereBmatchesB. It then moves left from(3, 4)to(3, 3), whereCmatchesC. -
One more left move reaches
(2, 1).BmatchesB, filling the first slot. Reading the slots left to right givesBCBA, length 4.
BCBA is one optimal answer. Another legal tie rule can recover BDAB, also length 4. The table proves the length; the chosen traceback path identifies one witness.
Time and space complexity
| Measure | Cost |
|---|---|
Naive enumeration from a | O(2^m × n) |
| DP table construction | O(m × n) |
| Full table space | O(m × n) |
| Traceback | O(m + n) |
Here m and n are the two string lengths. The naive bound assumes each generated subsequence of a is checked against b in O(n) time. If you need only the length, compress the DP table to two rows, or one row updated in place, for O(min(m, n)) space. Reconstruction needs more information, so this animation keeps the full table.
Edge cases
- If either string is empty, the answer is the empty sequence with length 0.
- If the strings share no characters, every non-base cell stays 0 and traceback emits nothing.
- Repeated characters can create several optimal paths. A fixed tie rule makes one result repeatable, not uniquely correct.
- Identical strings have the whole string as their LCS.
- LCS is a subsequence problem, not a substring problem. Selected characters may have gaps between them.
Common mistakes
- Using
max(top, left) + 1when characters match. A match extends the diagonal state. - Adding 1 on a mismatch. Different trailing characters have contributed no shared character.
- Treating an early zero tie as a meaningful branch decision. Both candidates are 0; write 0 and keep moving.
- Forgetting the empty-prefix row and column, which forces special cases into every boundary cell.
- Filling a cell before its top, left, and diagonal dependencies are ready.
- Assuming earliest matches are safe. On the default input, matching
Aata[0]tob[3]burns the prefix that supportsBCBA. - Calling the recovered letters unique. The optimal length is fixed, but several LCS strings may realize it.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Premium players need paid access. Every Explain doc is free.
Found a mistake or something unclear?