On this page
Compare ABCBDAB with BDCABA. You can strike out letters from each until the two leftovers read the same: BCBA survives in both, and nothing longer does, so the longest common subsequence has length 4. Dynamic programming finds this by filling one small table.
What is the longest common subsequence?
A subsequence keeps some characters of a string in their original order and may skip others. BCBA is a subsequence of ABCBDAB (positions 2, 3, 4, 6) and of BDCABA (positions 1, 3, 5, 6). A common subsequence appears in both strings, and the LCS is a longest one. Several different LCSs can share the same length.
What is the DP recurrence for LCS?
Call the strings X (length m) and Y (length n), and let c[i][j] be the LCS length of the first i characters of X and the first j characters of Y. Hunt and McIlroy describe this simple scheme, crediting earlier work, in their 1976 Bell Labs report on the algorithm behind diff:
c[i][0] = 0 and c[0][j] = 0, because an empty string shares nothing.
If X[i] = Y[j], then c[i][j] = c[i-1][j-1] + 1: the matching character extends the best answer without it.
Otherwise c[i][j] = max(c[i-1][j], c[i][j-1]): drop the last character of one string or the other and keep the better result.
The answer is c[m][n], and the same report notes the table costs O(mn) time and space. Wagner and Fischer's 1974 algorithm for the string-to-string correction problem also runs in time proportional to the product of the lengths and lists the LCS as an application.
How do you fill the LCS table for ABCBDAB and BDCABA?
Put X = ABCBDAB down the side (rows 1 to 7) and Y = BDCABA across the top (columns 1 to 6). Row 0 and column 0 are zeros. Fill row by row, left to right; each line below lists c[i][1] to c[i][6]:
Row 1 (A): 0 0 0 1 1 1
Row 2 (B): 1 1 1 1 2 2
Row 3 (C): 1 1 2 2 2 2
Row 4 (B): 1 1 2 2 3 3
Row 5 (D): 1 2 2 2 3 3
Row 6 (A): 1 2 2 3 3 4
Row 7 (B): 1 2 2 3 4 4
Check one cell: at row 4 (B), column 5 (B) the characters match, so take the diagonal c[3][4] = 2 and add 1. The bottom-right cell is 4, the LCS length.
How do you trace back to get the actual subsequence?
Start at c[7][6] and walk back to the edge:
If the two characters match, write the character down and move diagonally up-left.
If they don't, move to whichever neighbour (above or left) holds the larger value. On a tie, go up.
Stop when you reach row 0 or column 0, then reverse what you wrote.
At c[7][6] (B against A) both neighbours hold 4, so you go up. At c[6][6] the A's match: write A. Continuing, you pick up B at c[4][5], C at c[3][3] and B at c[2][1]. Reversed, that reads BCBA, the path in the diagram above. Breaking ties to the left instead gives another LCS of length 4, BDAB.
To practise more DP tables, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
How does LCS power diff tools and DNA comparison?
Treat each line of a file as one character. Hunt and McIlroy write that the goal of diff is to report the minimum number of line changes, which is the same as finding the longest common subsequence of lines in both files. Lines in the LCS stay; everything else is reported as added or deleted. Today's tools use other methods: the GNU diff manual says its basic algorithm is Eugene Myers' 1986 difference algorithm. Hirschberg showed in 1975 that an LCS can be found in quadratic time but only linear space, which matters when the inputs are long.
In biology the characters are bases. A 2019 paper in BMC Research Notes describes finding the LCS as an important problem in bioinformatics for DNA sequence alignment and pattern discovery. The same paper notes that finding the LCS among sequences is NP-hard in general; for two strings the table above is enough. Comparing sequences is also how a molecular clock dates species splits from DNA.
What is the difference between a subsequence and a substring?
A substring must be contiguous, but a subsequence need not be. For the longest common substring, the recurrence changes in one place: on a mismatch the cell resets to 0 instead of taking a max, and the answer is the largest value anywhere in the table, not the corner. For our pair, the longest common substring has length 2 (AB or BD), while the LCS has length 4.
For more ordered patterns inside sequences, read the Erdős–Szekeres theorem on monotone subsequences. For the theory behind recurrences and sequences, the Fermi Concept Library gives each concept its own lesson and its place in the map.



