2Brute2Best
🔥0

Longest Palindromic Subsequence

Medium·LeetCode #516 Pro OnlySlight Variation
LeetCode
01

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.

Input

A string s of lowercase letters.

Output

The length of the longest palindromic subsequence of s.

Example 1
Input: s = "bbbab"
Output: 4

bbbb is a palindromic subsequence. Take the bs at positions 0, 1, 2 and 4, and skip the a.

Example 2
Input: s = "cbbd"
Output: 2

bb is the longest one. No longer palindrome can be made from the letters in order.

Constraints
  • • 1 ≤ s.length ≤ 1000
  • • s consists only of lowercase English letters.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“longest palindromic subsequence”
→ Maximize the length of a palindrome made by deleting letters. The answer is a best value over all choices.
“subsequence (letters need not be next to each other)”
→ Letters can be skipped, so each range's answer is built from the answers for smaller ranges, not from one piece of text.
“return the length”
→ The answer is a number, so the table stores lengths. No text needs to be built.
03

Approach Complexity Ladder

Brute Force — recursion on the two endsO(2^N)O(N)

For s[i..j]: if the ends match, use both; if not, drop one end and try both

Better — table dp[i][j] for every rangeO(N^2)O(N^2)

dp[i][j] is the answer for s[i..j]; fill the table from the last row up

Optimal — one row, updated from right to leftO(N^2)O(N)

Keep one row of the table, and save the old diagonal value before each cell is overwritten

Brute Force Intuition

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:

dp[i][j] for the substring s[i..j]; expand from the centre or fill by increasing length.
Remember this · DP — Strings
Trigger
Longest common subsequence, edit distance, palindromes, matching with wildcards, interleaving.
Think
Look at the last characters: if they match, what do I do? If not, what are my options?
Key question
What does dp[i][j] mean, and what are the empty-string base cases?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.