A substring is a palindrome when it reads the same left-to-right and right-to-left. Given a string, count how many of its substrings are palindromes. Two substrings that occupy different index ranges are counted separately even if they contain the same characters, so every single character contributes one palindrome.
A single line containing the string, made up of lowercase English letters.
A single integer: the number of substrings (counted by their start and end positions) that are palindromes.
a-z.Example 1
Input
aaa
Expected
6
Explanation
The palindromes are a, a, a, aa, aa, and aaa, which is 6 in total.
Example 2
Input
abacaba
Expected
12
Explanation
The 7 single letters, plus aba, aca, aba, bacab, and abacaba, give 12 palindromic substrings.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →