MediumStrings · Two Pointers · DP

Longest Palindromic Substring

Given a string s, return its longest substring that reads the same forwards and backwards. If several have the same length, return any of them.

s = "ababd" → "bab" (or "aba")s = "abbc" → "bb"

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

The string

Dashed box = substring being tested; gold bracket = longest so far

Time and space complexity

n = len(s). The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Letters and digits; drawings use at most 9 characters for brute force, 10 for the DP table and 16 for expand around centre.