Open In App

Palindrome Substrings Count using Center Expansion

Last Updated : 15 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string, the task is to find the count of all the palindromic substrings in a given string with a length greater than or equal to 2.

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. In this article, we’ve implemented an approach based on centre expansion technique.

[Expected 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.
C++
// C++ program to count all palindromic 
// substrings of a given string
#include <bits/stdc++.h>
using namespace std;

// Function to count all palindromic substrings
int countPalindromes(string& s) {

    int n = s.size();
    int count = 0;
  
    // Count odd length palndrome substrings 
    // with str[i] as center.
    for (int i = 0; i < s.size(); i++) {
        int left = i - 1;
        int right = i + 1;
        while (left >= 0 and right < n) {
            if (s[left] == s[right])
                count++;
            else
                break;
            left--;
            right++;
        }
    }

    // Count even length palindrome substrings
    // where str[i] is first center.
    for (int i = 0; i < s.size(); i++) {
        int left = i;
        int right = i + 1;
        while (left >= 0 and right < n) {
            if (s[left] == s[right])
                count++;
            else
                break;
            left--;
            right++;
        }
    }
    return count;
}

int main() {
    string s = "abbaeae";
    cout << countPalindromes(s);
    return 0;
}
Java
// Java program to count all palindromic 
// substrings of a given string
class GFG {

    // Function to count all palindromic substrings
    static int countPalindromes(String s) {

        int n = s.length();
        int count = 0;

        // Count odd length palindrome substrings 
        // with str[i] as center.
        for (int i = 0; i < s.length(); i++) {
            int left = i - 1;
            int right = i + 1;
            while (left >= 0 && right < n) {
                if (s.charAt(left) == s.charAt(right))
                    count++;
                else
                    break;
                left--;
                right++;
            }
        }

        // Count even length palindrome substrings
        // where str[i] is first center.
        for (int i = 0; i < s.length(); i++) {
            int left = i;
            int right = i + 1;
            while (left >= 0 && right < n) {
                if (s.charAt(left) == s.charAt(right))
                    count++;
                else
                    break;
                left--;
                right++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String s = "abbaeae";
        System.out.println(countPalindromes(s));
    }
}
Python
# Python program to count all palindromic 
# substrings of a given string

# Function to count all palindromic substrings
def countPalindromes(s):

    n = len(s)
    count = 0

    # Count odd length palindrome substrings 
    # with str[i] as center.
    for i in range(len(s)):
        left = i - 1
        right = i + 1
        while left >= 0 and right < n:
            if s[left] == s[right]:
                count += 1
            else:
                break
            left -= 1
            right += 1

    # Count even length palindrome substrings
    # where str[i] is first center.
    for i in range(len(s)):
        left = i
        right = i + 1
        while left >= 0 and right < n:
            if s[left] == s[right]:
                count += 1
            else:
                break
            left -= 1
            right += 1

    return count

# Driver code
s = "abbaeae"
print(countPalindromes(s))
C#
// C# program to count all palindromic 
// substrings of a given string
using System;

class GFG {

    // Function to count all palindromic substrings
    static int countPalindromes(string s) {

        int n = s.Length;
        int count = 0;

        // Count odd length palindrome substrings 
        // with str[i] as center.
        for (int i = 0; i < s.Length; i++) {
            int left = i - 1;
            int right = i + 1;
            while (left >= 0 && right < n) {
                if (s[left] == s[right])
                    count++;
                else
                    break;
                left--;
                right++;
            }
        }

        // Count even length palindrome substrings
        // where str[i] is first center.
        for (int i = 0; i < s.Length; i++) {
            int left = i;
            int right = i + 1;
            while (left >= 0 && right < n) {
                if (s[left] == s[right])
                    count++;
                else
                    break;
                left--;
                right++;
            }
        }
        return count;
    }

    public static void Main() {
        string s = "abbaeae";
        Console.WriteLine(countPalindromes(s));
    }
}
JavaScript
// JavaScript program to count all palindromic 
// substrings of a given string

// Function to count all palindromic substrings
function countPalindromes(s) {

    let n = s.length;
    let count = 0;

    // Count odd length palindrome substrings 
    // with str[i] as center.
    for (let i = 0; i < s.length; i++) {
        let left = i - 1;
        let right = i + 1;
        while (left >= 0 && right < n) {
            if (s[left] === s[right])
                count++;
            else
                break;
            left--;
            right++;
        }
    }

    // Count even length palindrome substrings
    // where str[i] is first center.
    for (let i = 0; i < s.length; i++) {
        let left = i;
        let right = i + 1;
        while (left >= 0 && right < n) {
            if (s[left] === s[right])
                count++;
            else
                break;
            left--;
            right++;
        }
    }
    return count;
}

// Driver code
let s = "abbaeae";
console.log(countPalindromes(s));

Output
4

Time complexity: O(n^2) in worst case because we are running one while loop inside a for loop because in while loop we are expanding to both directions so that’s why it’s  O(n/2) in worst case when all characters are same eg : “aaaaaaaa” .
Auxiliary Space: O(1) . 

[Optimized Approach] – Using Manachar’s Algorithm – O(n) Time and O(n) Space

The idea of above center expansion can be further optimized using Manachar’s algorithm. Please refer this cop-algorithms article for details and implementation.

Time Complexity: O(n), where n is the size of the string.
Space Complexity: O(n)



Next Article
Practice Tags :

Similar Reads