LC 5
LeetCode 5 · Medium

Longest Palindromic Substring

Return the longest palindromic substring.


Try it

Step through the core mechanic. The simulator below runs the dynamic programming shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is Dynamic programming — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

Expand around each of the 2n−1 centres (single chars and gaps), growing while both ends match. O(n²) time, O(1) space, and simpler than the DP table. (Manacher's is the O(n) version.)

AspectValue
PatternDynamic programming
Recognise it byLongest substring that reads the same both ways.
Time complexityO(n²)
Space complexityO(1)
DifficultyMedium

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.