Palindrome Substrings Count using Center Expansion
Last Updated :
15 Feb, 2025
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));
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)
Similar Reads
Palindrome Substrings Count Given a string s, the task is to count all palindromic substrings of length more than one.Examples:Input: s = "abaab"Output: 3Explanation: Palindromic substrings with length greater than 1, are "aba", "aa", and "baab".Input: s = "aaa"Output: 3Explanation: Palindromic substrings with length greater t
15+ min read
Count Palindromic Substrings in a Binary String Given a binary string S i.e. which consists only of 0's and 1's. Calculate the number of substrings of S which are palindromes. String S contains at most two 1's. Examples: Input: S = "011"Output: 4Explanation: "0", "1", "1" and "11" are the palindromic substrings. Input: S = "0" Output: 1Explanatio
7 min read
Count of Palindrome Strings in given Array of strings Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to return the count of all palindromic string in the array. Examples: Input: arr[] = {"abc","car","ada","racecar","cool"}Output: 2Explanation: "ada" and "racecar" are the two palindrome
5 min read
Length of Longest Palindrome Substring Given a string S of length N, the task is to find the length of the longest palindromic substring from a given string. Examples: Input: S = "abcbab"Output: 5Explanation: string "abcba" is the longest substring that is a palindrome which is of length 5. Input: S = "abcdaa"Output: 2Explanation: string
15+ min read
Count of Palindromic substrings in an Index range Given a string str of small alphabetic characters other than this we will be given many substrings of this string in form of index tuples. We need to find out the count of the palindromic substrings in given substring range. Examples: Input : String str = "xyaabax" Range1 = (3, 5) Range2 = (2, 3) Ou
11 min read
Longest Palindromic Substring using hashing in O(nlogn) Given a string S, The task is to find the longest substring which is a palindrome using hashing in O(N log N) time. Input: S: âforgeeksskeegforâ, Output: âgeeksskeegâ Input: S: âGeeksâ, Output: âeeâ Hashing to Solve the Problem:The hashing approach to solving the longest palindromic substring proble
11 min read
Longest Palindromic Substring using Palindromic Tree | Set 3 Given a string, find the longest substring which is a palindrome. For example, if the given string is âforgeeksskeegforâ, the output should be âgeeksskeegâ. Prerequisite : Palindromic Tree | Longest Palindromic Substring Structure of Palindromic Tree : The palindromic Treeâs actual structure is clos
15+ min read
Count all palindromic Substrings for each character in a given String Given a string S of length n, for each character S[i], the task is to find the number of palindromic substrings of length K such that no substring should contain S[i], the task is to return an array A of length n, where A[i] is the count of palindromic substrings of length K which does not include t
9 min read
Count of non-palindromic strings of length M using given N characters Given two positive integers N and M, the task is to calculate the number of non-palindromic strings of length M using given N distinct characters. Note: Each distinct character can be used more than once. Examples: Input: N = 3, M = 2 Output: 6 Explanation: Since only 3 characters are given, those 3
8 min read
Count substrings of a given string whose anagram is a palindrome Given a string S of length N containing only lowercase alphabets, the task is to print the count of substrings of the given string whose anagram is palindromic. Examples: Input: S = "aaaa"Output: 10Explanation:Possible substrings are {"a", "a", "a", "a", "aa", "aa", "aa", "aaa", "aaa", "aaaa"}. Sinc
10 min read