Introduction
Three classical DP problems act on sequences: the longest common subsequence (LCS) of two strings, the edit distance (Levenshtein distance) between two strings, and the longest increasing subsequence (LIS) of one sequence. Each fills a table whose state is a pair of prefixes (or a single prefix with an auxiliary structure), and each is a standard GATE dynamic-programming question.
This note states the recurrences, works a full LCS table with traceback, and records the and routes to LIS.
Prerequisites
- Dynamic Programming Fundamentals: optimal substructure, overlapping subproblems, table fill and reconstruction.
Core concepts
Subsequence versus substring
A subsequence preserves order but need not be contiguous. A substring (or subarray) is contiguous. LCS and LIS ask for subsequences. A string of length has subsequences (counting repeats), which is why brute force over subsequences is hopeless and DP is needed.
Longest common substring is different. Its state is "longest common suffix of and ": if , and otherwise. The answer is the maximum entry of the table, not the bottom-right entry. For ABCBDAB and BDCABA the longest common substring has length 2 (for example AB), while the LCS has length 4.
State for two strings
For strings and , the natural state is "answer for the prefixes and " with and . That gives an table. Base row and column correspond to an empty prefix.
Algorithms and pseudocode
Pseudocode uses 1-based arrays, <- for assignment and X[i] for the -th character of .
Longest common subsequence
LCS-LENGTH(X, Y)
1 m <- X.length; n <- Y.length
2 let c[0..m, 0..n] be a new table
3 for i <- 0 to m
4 c[i, 0] <- 0
5 for j <- 0 to n
6 c[0, j] <- 0
7 for i <- 1 to m
8 for j <- 1 to n
9 if X[i] = Y[j] then
10 c[i, j] <- c[i - 1, j - 1] + 1
11 else c[i, j] <- max(c[i - 1, j], c[i, j - 1])
12 return c[m, n]Time / space. time and space. Length alone needs only the previous row: extra space. Reconstructing one LCS walks from toward : on a match, collect and step diagonally; reverse the collected characters before output; otherwise step to the neighbour of equal value.
Edit distance
Allowed operations: insert one character, delete one character, substitute one character (each cost 1). Let be the edit distance of and .
(The three options are delete, insert and substitute.) Time . If substitute is forbidden, drop the diagonal option and force a delete-plus-insert of cost 2 when characters differ.
Link to LCS. With insertions and deletions only, the distance is exactly : keep a longest common subsequence, delete the other characters of and insert the other characters of .
Longest increasing subsequence
Related material. LIS is not named separately in the GATE syllabus; it is included because it is the standard one-sequence DP companion of LCS.
DP. Let be the length of a longest increasing subsequence ending at index . Then , or if no such . Answer . Time .
patience method. Maintain an array tail[k] = smallest tail value of any increasing subsequence of length seen so far. For each , binary-search the first tail entry and replace it (or append). The length of tail is the LIS length. Time . Strict versus non-decreasing is controlled by using versus in the search.
LCS of with a sorted copy of (duplicates removed, for a strictly increasing LIS) also yields LIS in and makes the connection to LCS explicit (CLRS Exercise 15.4-5).
Mathematical explanation
Correctness sketch for LCS
Any LCS of and either ends with the matched pair (and the rest is an LCS of the shorter prefixes) or omits or omits . Those three cases partition the possibilities and justify the recurrence (CLRS 3e, Section 15.4).
Why edit distance is DP
An optimal alignment of the two prefixes ends with a match/substitute of the last characters, a deletion of , or an insertion of . Optimal substructure follows by the usual cut-and-paste argument; the table has overlapping states.
Worked examples
LCS of ABCBDAB and BDCABA
A B C B D A B (), B D C A B A ().
The length table (rows , columns ):
| B | D | C | A | B | A | ||
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| A | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| B | 0 | 1 | 1 | 1 | 1 | 2 | 2 |
| C | 0 | 1 | 1 | 2 | 2 | 2 | 2 |
| B | 0 | 1 | 1 | 2 | 2 | 3 | 3 |
| D | 0 | 1 | 2 | 2 | 2 | 3 | 3 |
| A | 0 | 1 | 2 | 2 | 3 | 3 | 4 |
| B | 0 | 1 | 2 | 2 | 3 | 4 | 4 |
Length . The backward traceback collects ABCB; reversing that buffer gives the LCS BCBA. Exactly three distinct strings are LCSs here: BCAB, BCBA and BDAB. A traceback that prefers "up" on ties and one that prefers "left" can return different strings; both are correct.

Edit distance of KITTEN and SITTING
Distance 3. The DP table's bottom-right entry is 3. With insertions and deletions only, the distance becomes , since (ITTN).
A full edit-distance table: ABC to YABD
Rows are prefixes of ABC, columns are prefixes of YABD; the first row and column are the base cases and .
| Y | A | B | D | ||
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | |
| A | 1 | 1 | 1 | 2 | 3 |
| B | 2 | 2 | 2 | 1 | 2 |
| C | 3 | 3 | 3 | 2 | 2 |
: insert Y in front, keep A and B, substitute C by D. Cell comes from the match A = A on the diagonal: .
LIS of 10, 22, 9, 33, 21, 50, 41, 60
: lengths ending at each index are . LIS length 5, for example .
Patience method (replace the first tail entry the new value, else append):
| Read | 10 | 22 | 9 | 33 | 21 | 50 | 41 | 60 |
|---|---|---|---|---|---|---|---|---|
tail after | 10 | 10, 22 | 9, 22 | 9, 22, 33 | 9, 21, 33 | 9, 21, 33, 50 | 9, 21, 33, 41 | 9, 21, 33, 41, 60 |
The final size is 5, the LIS length. The final tail array is not itself an increasing subsequence of the input: 9, 21, 33, 41, 60 would need 33 to come after 21, but 33 appears earlier. Recovering an actual LIS needs predecessor pointers.
Alignment view of edit distance
Edit distance equals the minimum cost of an alignment of the two strings, where a column is a matched or substituted pair, a deletion (character of against a gap), or an insertion (gap against a character of ). The DP recurrence evaluates the best last column of any alignment of the two prefixes. This view makes the three-way memorable: diagonal = substitute or match, up = delete, left = insert.
Important properties and observations
- LCS length is symmetric: . The specific subsequence recovered depends on traceback ties.
- Empty-string base cases are zeros for LCS and index values for edit distance; mixing them is a common error.
- The LPS length equals the LCS length of a string and its reverse: . To recover a palindrome, use an LPS recurrence or a reconstruction guaranteed to return a palindrome; an arbitrary LCS need not be palindromic. For BBABCBCAB it is 7 (BABCBAB). CLRS Problem 15-2 treats this problem.
- The unmemoized LCS recursion is exponential in the worst case (when no characters match, each call spawns two calls); the table has only distinct states.
- LIS returns the length easily; recovering the subsequence needs predecessor pointers.
Common misconceptions.
- "LCS is contiguous." That would be the longest common substring.
- "Edit distance always allows substitution at cost 1." Some questions set substitute cost to 2 (delete + insert) or forbid it; read the statement.
- "LIS requires strictly increasing." Confirm strict versus non-decreasing in the question.
GATE examination perspective
- COMPLETE THE TABLE. A question may give a partially filled LCS or edit-distance table; compute the next cell from the recurrence.
- TRACEBACK. State whether ties prefer up or left; both are correct for length.
- COMPLEXITY. for LCS and edit distance; or for LIS.
- VARIANT COSTS. Insertion, deletion and substitution may have unequal costs; the recurrence becomes a weighted .
- COUNT THE LCSs. "How many distinct LCSs?" means distinct strings, not distinct traceback paths; enumerate carefully (three for ABCBDAB and BDCABA).
- SUBSTRING OR SUBSEQUENCE. Longest common substring resets to 0 on a mismatch and takes the table maximum.
- INSERT/DELETE ONLY. Distance .
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 15, Section 15.4 (longest common subsequence); Exercise 15.4-5 (LIS via LCS) and Exercise 15.4-6 (O(n lg n) LIS); Problem 15-2 (longest palindrome subsequence).