Longest Palindromic Substring
Problem Statement
You are given a string s. Return the longest part of s that is a palindrome. The part must be a substring, which means one run of letters that sit next to each other in s. A palindrome reads the same forwards and backwards.
If more than one palindrome has the longest length, any one of them is a correct answer.
A string s made of letters and digits.
A substring of s that is a palindrome and has the largest possible length.
aba has the same length, so it is also a correct answer.
bb is the longest palindrome. The two bs sit side by side, so nothing needs to be skipped.
- • 1 ≤ s.length ≤ 1000
- • s consists of only digits and English letters.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check every substring from both ends, and remember the longest one that passes
Build isPal[i][j] by length, and remember the longest cell that is true
From each of the 2N − 1 centres, grow outward while the ends match; keep the longest
Try every substring s[i..j]. Check it from both ends, moving inward. If it is a palindrome and longer than the best so far, store its start and length, not the text. At the end, cut the answer out of s once.
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.