Problem Statement
Longest Palindromic Substring
A palindrome is a word that reads the same forwards and backwards, like "mom" or "racecar". A substring is just a chunk of letters that sit next to each other inside a bigger string. So the job is, given a string s, find the longest chunk inside it that reads the same both ways. The trick we use is called expand around center. Picture a palindrome as something with a middle point, where the left side is a mirror image of the right side. So we pick a middle, then step outward one letter at a time on both sides, and as long as the two letters match, we keep going. Why does this fit so well? Because every palindrome has exactly one center, so if we try every possible center we cannot miss any palindrome. There are 2n-1 possible centers in a string of length n, one sitting on each letter (for odd-length palindromes like "aba"), and one sitting in each gap between two letters (for even-length palindromes like "bb"). For each center we expand outward while the letters match, and we remember the longest palindrome we have seen. This runs in O(n^2) time using only O(1) extra space, which is lighter on memory than the dynamic programming version that needs an O(n^2) table.
Signals to notice
Brute force first
Check every substring for palindrome. Each substring is independently verified. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
The key insight
Expand around center: for each position, expand outward while characters match. Try both odd-length (single center) and even-length (double center). Track the longest. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
Trace it on s="cbbd"
init: start=0, end=0 (best span len 1) i=0: expand(0,0)→"c" [0,0]; expand(0,1)→ c≠b fail; no update → start,end=0,0 i=1: expand(1,1)→"b" [1,1]; expand(1,2)→ b==b then c≠d → "bb" [1,2], span 1>0 → start,end=1,2 i=2: expand(2,2)→"b" [2,2] span 0≯1; expand(2,3)→ b≠d fail; no update i=3: expand(3,3)→"d" [3,3] span 0≯1; expand(3,4)→ right out of bounds → no update return s[start:end+1] = s[1:3] = "bb"
What must stay true
Every palindrome has a center (single character for odd length, pair for even). Expanding from the center outward checks the palindrome condition naturally — stop when characters don't match. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Shape of the loop
start, end = 0, 0
for i in range(len(s)):
l1, r1 = expand(i, i) # odd-length center
l2, r2 = expand(i, i+1) # even-length center
if r1 - l1 > end - start: start, end = l1, r1
if r2 - l2 > end - start: start, end = l2, r2
return s[start:end+1] # expand: widen [l,r] while s[l]==s[r]Pseudocode only — the full worked solution lives in the Solution tab.
Easy way to go wrong
Only checking odd-length palindromes — 'abba' is even-length and has no single center. You must try both odd and even centers at each position. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.