Find the count of palindromic sub-string of a string in its sorted form
Last Updated :
11 Jul, 2025
Given string str consisting of lowercase English alphabets, the task is to find the total number of palindromic sub-strings present in the sorted form of str.
Examples:
Input: str = "acbbd"
Output: 6
All palindromic sub-string in it's sorted form ("abbcd") are "a", "b", "b", "bb", "c" and "d".
Input: str = "abbabdbd"
Output: 16
Naive approach: One way is to sort the given string and then count the total number of sub-strings present which are palindromes. For finding a number of palindromic sub-strings this approach can be used which has a time complexity of O(n^2).
Optimized approach: An efficient way is to count the frequency of each character and then for each frequency total number of palindromes will (n*(n+1))/2 as all the palindromic sub-strings of a sorted string will consist of the same character.
For example, palindromic sub-string for the string "aabbbcd" will be "a", "aa", ..., "bbb", "c", ... etc. Time complexity for this approach will be O(n).
- Create a hash table for storing the frequencies of each character of the string str.
- Traverse the hash table and for each non-zero frequency add (hash[i] * (hash[i]+1)) / 2 to the sum.
- Print the sum in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
// function to return count of palindromic sub-string
int countPalindrome(string str)
{
int n = str.size();
int sum = 0;
// initialize frequency table with 0
int hashTable[MAX_CHAR] = {0};
// calculate frequency
for (int i = 0; i < n; i++)
hashTable[str[i] - 'a']++;
// calculate count of palindromic sub-string
for (int i = 0; i < 26; i++) {
if (hashTable[i])
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
// return result
return sum;
}
// driver program
int main()
{
string str = "ananananddd";
cout << countPalindrome(str);
return 0;
}
Java
// Java program to find the count of palindromic sub-string
// of a string in it's ascending form
class GFG {
final static int MAX_CHAR = 26;
// function to return count of palindromic sub-string
static int countPalindrome(String str) {
int n = str.length();
int sum = 0;
// calculate frequency
int hashTable[] = new int[MAX_CHAR];
for (int i = 0; i < n; i++) {
hashTable[str.charAt(i) - 'a']++;
}
// calculate count of palindromic sub-string
for (int i = 0; i < 26; i++) {
if (hashTable[i] != 0) {
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
}
// return result
return sum;
}
// driver program
public static void main(String[] args) {
String str = "ananananddd";
System.out.println(countPalindrome(str));
}
}
Python
# Python3 program to find the count of
# palindromic sub-string of a string
# in it's ascending form
MAX_CHAR = 26
# function to return count of
# palindromic sub-string
def countPalindrome(str):
n = len (str)
sum = 0
# calculate frequency
hashTable = [0] * MAX_CHAR
for i in range(n):
hashTable[ord(str[i]) -
ord('a')] += 1
# calculate count of palindromic
# sub-string
for i in range(26) :
if (hashTable[i]):
sum += (hashTable[i] *
(hashTable[i] + 1) // 2)
# return result
return sum
# Driver Code
if __name__ == "__main__":
str = "ananananddd"
print (countPalindrome(str))
# This code is contributed by ita_c
C#
// C# program to find the count of palindromic sub-string
// of a string in it's ascending form
using System;
public class GFG{
readonly static int MAX_CHAR = 26;
// function to return count of palindromic sub-string
static int countPalindrome(String str) {
int n = str.Length;
int sum = 0;
// calculate frequency
int []hashTable = new int[MAX_CHAR];
for (int i = 0; i < n; i++) {
hashTable[str[i] - 'a']++;
}
// calculate count of palindromic sub-string
for (int i = 0; i < 26; i++) {
if (hashTable[i] != 0) {
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
}
// return result
return sum;
}
// driver program
public static void Main() {
String str = "ananananddd";
Console.Write(countPalindrome(str));
}
}
// This code is contributed by Rajput-Ji
JavaScript
// Javascript program to find the count of palindromic sub-string
// of a string in it's ascending form
var MAX_CHAR = 26;
// function to return count of palindromic sub-string
function countPalindrome(str)
{
var n = str.length;
var sum = 0;
// calculate frequency
var hashTable = Array(MAX_CHAR).fill(0);
for (var i = 0; i < n; i++)
hashTable[str[i].charCodeAt(0) - 'a'.charCodeAt(0)]++;
// calculate count of palindromic sub-string
for (var i = 0; i < 26; i++) {
if (hashTable[i])
sum += (hashTable[i] * (hashTable[i] + 1) / 2);
}
// return result
return sum;
}
// driver program
var str = "ananananddd";
console.log( countPalindrome(str));
PHP
<?php
// PHP program to find the count of
// palindromic sub-string of a string
// in it's ascending form
$MAX_CHAR = 26;
// function to return count of
// palindromic sub-string
function countPalindrome($str)
{
global $MAX_CHAR;
$n = strlen($str);
$sum = 0;
// calculate frequency
$hashTable = array_fill(0, $MAX_CHAR, 0);
for ($i = 0; $i < $n; $i++)
$hashTable[ord($str[$i]) - ord('a')]++;
// calculate count of palindromic sub-string
for ($i = 0; $i < 26; $i++)
{
if ($hashTable[$i])
$sum += (int)($hashTable[$i] *
($hashTable[$i] + 1) / 2);
}
// return result
return $sum;
}
// Driver Code
$str = "ananananddd";
echo countPalindrome($str);
// This code is contributed by mits
?>
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the length of the string.
- Auxiliary Space: O(26), as we are using extra space for the hash table.
Similar Reads
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
Number of strings of length N with no palindromic sub string Given two positive integers N, M. The task is to find the number of strings of length N under the alphabet set of size M such that no substrings of size greater than 1 is palindromic. Examples: Input : N = 2, M = 3 Output : 6 In this case, set of alphabet are 3, say {A, B, C} All possible string of
5 min read
Permutation of given string that maximizes count of Palindromic substrings Given a string S, the task is to find the permutation of the string such that palindromic substrings in the string are maximum.Note: There can be multiple answers for each string. Examples: Input: S = "abcb" Output: "abbc" Explanation: "abbc" is the string with maximum number of palindromic substrin
3 min read
Count pairs of non-overlapping palindromic sub-strings of the given string Given a string S. The task is to count the non-overlapping pairs of palindromic sub-strings S1 and S2 such that the strings should be S1[L1...R1] and S2[L2...R2] where 0 ? L1 ? R1 < L2 ? R2 < N. The task is to count the number of pairs of the non-overlapping palindromic sub-strings. Examples:
13 min read
Count of pairs of strings whose concatenation forms a palindromic string Given an array A[ ] consisting of N strings, the task is to count the number of pairs of possible strings that on merging forms a Palindromic String or can be rearranged to form a Palindromic String. Example : Input: N = 6, A[ ] = {aab, abcac, dffe, ed, aa, aade}Output: 6Explanation: All possible pa
9 min read
Count pairs of non overlapping Substrings of size K which form a palindromic String Given a string S of length l, the task is to count the number of pairs of two non-overlapping substrings of length K of the string S, such that on joining the two non-overlapping substrings the resultant string is a palindrome. Examples: Input: S = "abcdbadc", K = 2Output: 2 Explanation: Possible su
15 min read