Skip to the workspace
Pebellslearn by building
Sequence DP: the Diff Engine  ›  Lesson

Longest Common Subsequence & the Survival Score

Longest Common Subsequence 30 minFill a two-dimensional table to find the longest sequence two versions share, in order

dsa / Longest Common Subsequence & the Survival Score

Explore each scene at your own pace.

01 · Why position-by-position fails

Why position-by-position fails

Longest Common Subsequence & the Survival Score

Definition
The longest common subsequence of two sequences is the longest list of items that appear in both in the same order, though not necessarily next to each other. A two-dimensional table of prefix answers computes its length.
In plain words
Take two versions of a document and find the lines that survived from one to the other, in the same order, even if new lines were slipped in between. The more lines survive, the more alike the two versions are.

In short: the longest common subsequence (LCS) of two sequences is the longest list of items that appear in both, in the same order, but not necessarily side by side. A two-dimensional table, table[i][j] = the LCS length of the first i items of a and the first j items of b, fills by comparing the last item of each prefix: a match extends the diagonal by one, a miss carries the larger of the cell above or to the left. The bottom-right cell is the answer, and the diagonal steps taken to get there spell out the subsequence itself.

A three-box staircase labelled R, A, E connected by diagonal arrows, with a greyed dashed box off to the side marked C not-equal T showing a miss just carries the larger neighbour

Quarry keeps every revision of every document. When someone opens the history panel, the first question is simple: how much of the old version survived into the new one? A new paragraph in the middle shouldn't count as "everything after it changed", and a moved sentence shouldn't count as nothing changed. The measure that gets this right is the longest common subsequence (LCS): the longest list of lines that appear in both versions in the same order, not necessarily side by side.

This lesson fills the first real two-dimensional DP table in the course. Every diff you have ever read came out of a table like this one.

Why position-by-position fails

The obvious comparison lines the two versions up by index:

What does this print?
old = ["title", "intro", "body", "outro"]
new = ["title", "note", "intro", "body", "outro"]
same = sum(1 for a, b in zip(old, new) if a == b)
print(same)

A subsequence ignores position and keeps order. title, intro, body, outro appears in both lists in that order, so the survival count should be 4.

Scene 1 of 9
Project context and further resources
You're building a piece ofQuarry — version history
This piece — similarity(): Measures how much of a document survived from one revision to the next.
Scenario Quarry's history panel shows how much of a document survived each edit. The survival score counts the lines two revisions share, in order, and turns that into a 0-to-1 number.
similarity.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
similarity(old, new) → dictHow many lines survived between two revisions, and a 0-to-1 score.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.