Total Palindrome Substring Count
StringsAuto-graded · 24 test casesLast updated
Tests the ability to efficiently enumerate all palindromic substrings using center expansion and handle both odd and even-length cases.
Write a function that calculates the total number of palindromic substrings within a given string.
A substring is any contiguous sequence of characters from the string. A palindrome is a sequence that reads identically forwards and backwards.
Your function should count every palindromic substring, where substrings at different positions are counted separately even if they contain the same characters.
Constraints:
1 ≤ len(s) ≤ 2000sconsists of lowercase English letters
Examples
Example 1
Input: total_palindrome_count('abc')
Output: 3
Example 2
Input: total_palindrome_count('aaa')
Output: 6
Example 3
Input: total_palindrome_count('aba')
Output: 4
Example 4
Input: total_palindrome_count('abba')
Output: 6
Hints
Hint 1
Think about where palindromes are centered - they can be centered on a single character or between two characters
Hint 2
Try expanding outward from each possible center position while characters match
Hint 3
An O(n²) solution is acceptable given the constraint that the string length is at most 2000
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution uses center expansion: we recognize that every palindrome has a central point of symmetry. This center can be either a single character (for odd-length palindromes) or the boundary between two adjacent characters (for even-length palindromes).
For a string of length n, there are n possible single-character centers and n - 1 possible between-character centers, giving us 2n - 1 total centers to check.
Starting from each center, we expand outward symmetrically. As long as the characters at both ends match, we've found another palindrome and increment our counter. We continue expanding until characters no longer match or we reach the string boundaries.
Why This Works
The correctness relies on two observations:
-
Completeness: Every palindrome in the string will be discovered. Any palindrome
s[i..j]has a unique center - if it has odd length, the center is at index(i+j)/2; if even length, the center is between indices(i+j)//2and(i+j)//2 + 1. When we expand from that center, we will reach this palindrome because all intermediate substrings are also palindromes (a palindrome with its outer characters removed is still a palindrome). -
No duplicates: Each substring position
(i, j)belongs to exactly one center, and within each center's expansion, each step produces a unique pair of boundaries. Therefore, no palindrome is counted multiple times.
Complexity Analysis
Time Complexity: O(n²) in the worst case. We examine 2n - 1 centers, and each expansion can extend up to O(n) steps. The worst case occurs with strings like "aaaa..." where every substring is a palindrome. However, the total number of palindromes in such a string is n(n+1)/2, and we count each in constant time, so the complexity is still quadratic.
Space Complexity: O(1). We only use a few variables to track positions and the count. No additional data structures are needed.
Implementation
def total_palindrome_count(s: str) -> int:
n = len(s)
count = 0
for center in range(n):
# Expand around single-character center (odd-length palindromes)
left = right = center
while left >= 0 and right < n and s[left] == s[right]:
count += 1
left -= 1
right += 1
# Expand around between-character center (even-length palindromes)
left, right = center, center + 1
while left >= 0 and right < n and s[left] == s[right]:
count += 1
left -= 1
right += 1
return count