Longest Common Subsequence (LCS)
@lcsPseudo-code
function LCS(A, B):
m = length(A)
n = length(B)
# Step 1: Create DP table
L = 2D array of size (m+1) x (n+1)
# Step 2: Fill table
for i from 0 to m:
for j from 0 to n:
# Base case
if i == 0 or j == 0:
L[i][j] = 0
# Characters match
else if A[i-1] == B[j-1]:
L[i][j] = L[i-1][j-1] + 1
# Characters do not match
else:
L[i][j] = max(L[i-1][j], L[i][j-1])
return L[m][n]
- A The first sequence (like a string or list).
- B The second sequence.
- m The length of A.
- n The length of B.
- L A 2D table where L[i][j] stores the length of the LCS of A[0..i-1] and B[0..j-1].
- i Row index (used for sequence A).
- j Column index (used for sequence B).
- x Temporary variable sometimes used to store max values during computation.
- 0 Base value used when one sequence is empty.
Recipe Steps
Think of matching letters — You have two words and want the longest matching pattern in order.
Create a grid — Rows for A, columns for B.
Fill the first row and column with 0 because empty strings have no matches.
If letters match, take the diagonal value and add 1.
If they do not match, take the maximum from left or top.
Repeat until the grid is filled.
The bottom-right value is the length of the longest common subsequence.
Questions & Answers
- Used in DNA matching, diff tools, and text comparison.
- Tip: Think of it as building answers for smaller prefixes.
- Common mistake: Confusing subsequence with substring (substring must be continuous).
- You can reconstruct the actual sequence by backtracking through the table.
Related Algorithms
Decision Tree (ID3 / CART)
A Decision Tree builds a flowchart-like structure that splits data step by step using features, until it can make a clear prediction.
ViewFloyd-Warshall Algorithm
The Floyd-Warshall algorithm finds the shortest paths between all pairs of nodes by gradually improving paths using intermediate nodes.
ViewGradient Descent
Gradient Descent is a way for machines to learn from data by adjusting their predictions to get closer to the correct answer. It's like a never-ending staircase where the machine takes small steps down until it reaches the bottom, which is the optimal solution.
View