KatInterview

Total Palindrome Substring Count

StringsAuto-graded · 24 test cases
MediumAuto-gradedStringspalindromestwo-pointersstring-manipulation

Last 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) ≤ 2000
  • s consists 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