Given a 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.
Input: str = “acbbd”
All palindromic sub-string in it’s sorted form (“abbcd”) are “a”, “b”, “b”, “bb”, “c” and “d”.
Input: str = “abbabdbd”
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Make palindromic string non-palindromic by rearranging its letters
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Minimum length of substring whose rotation generates a palindromic substring
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Longest palindromic string possible after removal of a substring
- Minimum size substring to be removed to make a given string palindromic
- Rearrange string to obtain Longest Palindromic Substring
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Suffix Tree Application 6 - Longest Palindromic Substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.