DSA Notes
Master the LCS problem with 2D DP table construction, backtracking for the actual subsequence, and its application in diff utilities.
The Problem
Given two strings, find the length of the longest subsequence present in both. A subsequence is a sequence that can be derived from a string by deleting some (or no) characters without changing the order of remaining characters.
Example:
- String A: "ABCBDAB"
- String B: "BDCAB"
- LCS: "BCAB" (length 4)
Note: "BDAB" is also length 4. Multiple LCS solutions may exist.
Why LCS Matters
LCS is the foundation of:
- diff utilities (git diff, file comparison)
- DNA sequence alignment (bioinformatics)
- Spell checking and autocorrect
- Plagiarism detection
- Version control (finding what changed between file versions)
The DP Approach
State: dp[i][j] = length of LCS of A[0..i-1] and B[0..j-1]
Transition:
- If
A[i-1] == B[j-1]: characters match!dp[i][j] = dp[i-1][j-1] + 1 - Else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])(skip one character from either string)
Base: dp[0][j] = 0 and dp[i][0] = 0 (empty string has LCS 0 with anything)
Dry Run
| Reading | dp[4][4] = 3, LCS has length 3 |
| How dp[3][3] = 2: A[2]='C' == B[2]='C' | dp[2][2]+1 = 1+1 = 2 |
| How dp[4][4] = 3: A[3]='B' == B[3]='B' | dp[3][3]+1 = 2+1 = 3 |
Python Implementation
Backtracking Logic Explained
Starting from dp[m][n], trace back to reconstruct the LCS:
- If
A[i-1] == B[j-1]: this character is in the LCS. Record it, move diagonally (i-1, j-1). - If
dp[i-1][j] >= dp[i][j-1]: the LCS came from skipping A[i-1]. Move up (i-1, j). - Else: the LCS came from skipping B[j-1]. Move left (i, j-1).
C++ Implementation
Space Optimization
Since each row only depends on the previous row, we can use O(n) space:
Tradeoff: With space optimization, you lose the ability to backtrack for the actual subsequence (you would need to store more information or use a different technique).
Time and Space Complexity
- Time: O(m × n) where m and n are string lengths
- Space: O(m × n) for full table, O(min(m,n)) with space optimization
Application: Diff Utility
The diff command (and git diff) uses LCS to show differences between files:
- Find the LCS of the two files (line by line)
- Lines in the LCS are unchanged
- Lines only in the old file were deleted
- Lines only in the new file were added
Related Problems
- Shortest Common Supersequence: Length = m + n - LCS_length
- Edit Distance: Minimum edits to transform A to B (uses similar 2D DP)
- Longest Palindromic Subsequence: LCS of string and its reverse
- Minimum deletions to make strings equal: (m - LCS) + (n - LCS)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Longest Common Subsequence (LCS).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, dynamic, programming, longest
Related Data Structures & Algorithms Topics