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 number of palindromic sub-strings this approach can be used which has 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++

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP program to find the count of palindromic sub-string

// of a string in it's ascending form

#include <bits/stdc++.h>

usingnamespacestd;

constintMAX_CHAR = 26;

// function to return count of palindromic sub-string

intcountPalindrome(string str)

{

intn = str.size();

intsum = 0;

// calculate frequency

inthashTable[MAX_CHAR];

for(inti = 0; i < n; i++)

hashTable[str[i] - 'a']++;

// calculate count of palindromic sub-string

for(inti = 0; i < 26; i++) {

if(hashTable[i])

sum += (hashTable[i] * (hashTable[i] + 1) / 2);

}

// return result

returnsum;

}

// driver program

intmain()

{

string str = "ananananddd";

cout << countPalindrome(str);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to find the count of palindromic sub-string

// of a string in it's ascending form

classGFG {

finalstaticintMAX_CHAR = 26;

// function to return count of palindromic sub-string

staticintcountPalindrome(String str) {

intn = str.length();

intsum = 0;

// calculate frequency

inthashTable[] = newint[MAX_CHAR];

for(inti = 0; i < n; i++) {

hashTable[str.charAt(i) - 'a']++;

}

// calculate count of palindromic sub-string

for(inti = 0; i < 26; i++) {

if(hashTable[i] != 0) {

sum += (hashTable[i] * (hashTable[i] + 1) / 2);

}

}

// return result

returnsum;

}

// driver program

publicstaticvoidmain(String[] args) {

String str = "ananananddd";

System.out.println(countPalindrome(str));

}

}

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# 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

defcountPalindrome(str):

n =len(str)

sum=0

# calculate frequency

hashTable =[0] *MAX_CHAR

fori inrange(n):

hashTable[ord(str[i]) -

ord('a')] +=1

# calculate count of palindromic

# sub-string

fori inrange(26) :

if(hashTable[i]):

sum+=(hashTable[i] *

(hashTable[i] +1) //2)

# return result

returnsum

# Driver Code

if__name__ =="__main__":

str="ananananddd"

print(countPalindrome(str))

# This code is contributed by ita_c

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find the count of palindromic sub-string

// of a string in it's ascending form

usingSystem;

publicclassGFG{

readonlystaticintMAX_CHAR = 26;

// function to return count of palindromic sub-string

staticintcountPalindrome(String str) {

intn = str.Length;

intsum = 0;

// calculate frequency

int[]hashTable = newint[MAX_CHAR];

for(inti = 0; i < n; i++) {

hashTable[str[i] - 'a']++;

}

// calculate count of palindromic sub-string

for(inti = 0; i < 26; i++) {

if(hashTable[i] != 0) {

sum += (hashTable[i] * (hashTable[i] + 1) / 2);

}

}

// return result

returnsum;

}

// driver program

publicstaticvoidMain() {

String str = "ananananddd";

Console.Write(countPalindrome(str));

}

}

// This code is contributed by Rajput-Ji

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?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

functioncountPalindrome($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";

echocountPalindrome($str);

// This code is contributed by mits

?>

chevron_right

filter_none

Output:

26

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.