Longest common subsequence
How to find the longest run of letters two words share in the same order, gaps allowed: the match-or-not rule, the table it fills, the walk back, and why diff tools use it.
In 30 seconds
A subsequence is what remains of a word after crossing out some letters without reordering the rest. Two texts can share a long subsequence even when no long block of them is identical.
To find the longest one, compare the last letters. If they match, count one and shorten both words; if not, shorten one or the other and keep the better result.
Writing those small answers in a table gives the length, and walking back through it gives the letters. Diff tools do this with lines to show what changed between two files.
Longest common subsequence, step by step
Two words can have several longest common subsequences of the same length; the walk back finds one. Real diff tools use faster variants of this idea (such as Myers’ algorithm).
- Step 1: Subsequence, not substring. Cross out any letters of a word and read what is left, in order: that is a subsequence. PAE is one for PLANE, even though its letters are not next to each other. A substring is stricter: a block of neighbours, like LAN. Every substring is a subsequence, but not the other way round.
- Step 2: The longest one they share. Given two words, the longest common subsequence (LCS) is the longest string that is a subsequence of both. Join equal letters with lines that never cross, and count the lines. PLANE and PANEL share PANE, four letters; joining the two Ls as well would cross the other lines, which means breaking the order.
- Step 3: Compare the last letters. Trying every subsequence is hopeless: a word of n letters has up to 2 to the power n of them. Instead look only at the last letters. If they are equal, that letter ends the common part: count 1 and solve the rest. If not, at least one of them is unused: drop each in turn and keep the better result.
- Step 4: Fill the table: a match. Store every small answer in a table. A cell holds the LCS length of the letters of PLANE down to its row and the letters of PANEL up to its column; the blank row and column stand for an empty word and are all 0. Where the row and column letters are equal, the cell is the diagonal neighbour plus 1.
- Step 5: Fill the table: no match. Where the letters differ, copy the larger of the cell above (the row letter dropped) and the cell to the left (the column letter dropped). The last cell compares the whole words: E and L differ, so it takes the 4 on its left. The LCS has length 4, found by filling 36 cells, each from its neighbours.
- Step 6: Walk back to read it. The table holds lengths; the letters come from walking back from the last cell. Where the row and column letters match, keep that letter and step diagonally. Otherwise move to the larger neighbour, above or left. The matches come out last letter first: E, N, A, P. Reversed, that is PANE.
- Step 7: This is how diff works. A diff tool does this with lines of text instead of letters. It finds a longest common subsequence of the old and new file: those lines are unchanged. Old lines outside it are shown as removed, new lines outside it as added. Because the LCS is as long as possible, the list of changes is as short as possible.
Try it yourself
Learn this first
Learn next
- Suffix arraysSort every ending of a text once, then find any word in it by binary search. Intermediate
- Rabin–Karp algorithmFinding a pattern in a text by sliding a rolling hash along it. Intermediate
- Git branching strategiesGit Flow against trunk-based development: where work branches, merges and gets released. Beginner
Spotted a mistake on this page?