Palindromic Substrings

Count how many substrings of s are palindromes. Substrings at different positions count separately.

A way to picture it

Same ripples as before, but now you're a lifeguard counting them. Every time a ripple spreads one more ring without breaking, that ring is one more palindrome.

How to approach it

Expand around all 2n − 1 centers. Every successful expansion step is exactly one new palindrome, so just count them.

Example

Input: {"s":"abc"}. Output: 3.

Complexity: O(n²) time and O(1) space. 2n − 1 centers, each expanding up to n/2 steps.

This page includes an animated walkthrough, Java and Python solutions, and a question to check your understanding.