MediumStrings · Two Pointers · DP

Palindromic Substrings

Given a string s, return how many of its substrings are palindromes. Substrings at different positions count separately, even if they contain the same letters.

s = "abc" → 3 ("a", "b", "c")s = "aaa" → 6 ("a" ×3, "aa" ×2, "aaa")

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 = palindrome just counted

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.