VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-palindrome-sub-strings-string-set-2/

⇱ Palindrome Substrings Count - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Palindrome Substrings Count

Last Updated : 4 Aug, 2025

Given a string s, find the total number of palindromic substrings of length greater than or equal to 2 present in the string.
A substring is palindromic if it reads the same forwards and backwards.

Examples:

Input: s = "abaab"
Output: 3
Explanation: Palindrome substrings (of length > 1) are "aba" , "aa" , "baab"

Input : s = "aaa"
Output: 3
Explanation : Palindrome substrings (of length > 1) are "aa" , "aa" , "aaa"

Input : s = "abbaeae"
Output: 4
Explanation : Palindrome substrings (of length > 1) are "bb" , "abba" , "aea", "eae"

Note: We have already discussed a naive and dynamic programming based solution in the article Count All Palindrome Sub-Strings in a String.

[Approach] Using Center Expansion - O(n^2) Time and O(1) Space

The idea is to consider each character of the given string as midpoint of a palindrome and expand it in both directions to find all palindromes of even and odd lengths.

  • For odd length strings, there will be one center point
  • For even length strings, there will be two center points.
  • A character can be a center point of a odd length palindrome sub-string and/or even length palindrome sub-string.

Output
4

[Expected Approach] - Using Manacher's Algorithm

We use Manacher’s algorithm to find all palindromic substrings in linear time by computing the maximum radius of palindromes centered at each character (after modifying the string with separators). For each center, the number of palindromic substrings is proportional to half the radius. After summing over all centers, we subtract palindromic substrings of length 1 to count only those of length ≥ 2.

Step by Step Implementation:

  • Preprocess the string: Insert a separator (#) between characters and add sentinels (@ at the beginning and $ at the end) to avoid bounds checking.
    Example: "abba" → "@#a#b#b#a#$"
  • Initialize variables:
    => p[i] → stores the radius of the longest palindrome centered at position i
    => left, right → define the current longest palindrome boundaries in the modified string
  • Traverse the modified string: For each index i from left to right
    => Find the mirror position of i: mirror = left + (right - i)
    => If i is within the current palindrome window (i < right), initialize p[i] = min(p[mirror], right - i)
    => Try to expand the palindrome centered at i by comparing characters symmetrically on both sides
    => If the palindrome expands beyond right, update left = i - p[i] and right = i + p[i]
  • Count total palindromic substrings: For each p[i], number of palindromic substrings centered at i is ceil(p[i]/2). Add all such counts to get total palindromic substrings
  • Exclude single length palindromes: The number of palindromic substrings of length 1 is equal to the length n of the original string. Subtract n to get the final count of substrings with length ≥ 2

Output
4

Time Complexity: O(n)
Auxiliary Space: O(n), additional space is used for the modified string ms and the palindrome radius array p, both of size proportional to the original string length.

Comment