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.)
| Aspect | Value |
|---|---|
| Pattern | Dynamic programming |
| Recognise it by | Longest substring that reads the same both ways. |
| Time complexity | O(n²) |
| Space complexity | O(1) |
| Difficulty | Medium |
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.