Quant Memo
Coding/●●●●●

Count every palindromic substring

Given a string, count how many of its contiguous substrings are palindromes. Every single character counts as a palindrome, and different positions count separately even if the text is identical.

"abc"  -> 3     # "a", "b", "c"
"aaa"  -> 6     # "a","a","a","aa","aa","aaa"

Return the total count in O(n2)O(n^2) time. The brute force checks all O(n2)O(n^2) substrings at O(n)O(n) each, which is O(n3)O(n^3); do better.

Show a hint

You already know how to grow a palindrome from a center. What if, instead of tracking the longest one, you just tallied one every time an expansion succeeds?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions