Longest Palindromic Substring
Return the longest contiguous palindromic substring.
Why does this pattern fit?
Restate the exact job
Return the longest contiguous palindromic substring.
Every palindrome expands from one character or one gap.
O(n²) time · O(1) space
Checking only odd centers misses even-length palindromes.
How to solve Longest Palindromic Substring
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Return the longest contiguous palindromic substring.
Why Dynamic programming fits
Every palindrome expands from one character or one gap.
State to maintain
Best start/length and a pair of expansion pointers.
Transition
Expand around every odd and even center, updating the longest valid range.
Time and space
O(n²) time · O(1) space
Counterexample to the tempting mistake
Checking only odd centers misses even-length palindromes.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.