Longest Palindromic Substring

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Longest Palindromic Substring

Approach

Expand around every odd and even center and keep the longest palindrome span encountered.

Complexity Analysis

Time
O(n^2)
Space
O(1)

Pattern

Expand Around Center

Why It Works

The longest palindrome must appear from some center expansion, so checking all centers guarantees optimality.

Updated Mar 2026