On this page
Longest Common Subsequence
Classic 2D DP on strings.
Overview
Classic 2D DP on strings.
Example
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
// Top-down memoization pattern
Common Use Cases
- Optimization problems
- Counting problems
- String transformation
Pitfalls to Avoid
- Not identifying state variables
- Wrong base cases
Related Topics
- Recursion
- Greedy algorithms comparison
Best Practices
- Understand when to use longest common subsequence versus simpler alternatives
- Write unit tests covering edge cases and failure paths
- Follow Java conventions and prefer standard library APIs when available
- Profile before optimizing — measure impact in your specific workload