Longest Palindromic Subsequence
Problem Statement
You are given a string s. Find the length of its longest palindromic subsequence. A subsequence is made by deleting zero or more letters from s, without reordering the rest. Its letters do not need to sit next to each other in s. A palindrome reads the same forwards and backwards.
A string s of lowercase letters.
The length of the longest palindromic subsequence of s.
bbbb is a palindromic subsequence. Take the bs at positions 0, 1, 2 and 4, and skip the a.
bb is the longest one. No longer palindrome can be made from the letters in order.
- • 1 ≤ s.length ≤ 1000
- • s consists only of lowercase English letters.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For s[i..j]: if the ends match, use both; if not, drop one end and try both
dp[i][j] is the answer for s[i..j]; fill the table from the last row up
Keep one row of the table, and save the old diagonal value before each cell is overwritten
For the range s[i..j]: if s[i] = s[j], both ends can be used, so the answer is 2 plus the answer for the inside. If they differ, the palindrome can use at most one of them, so drop the left end or the right end, and take the better of the two.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.