Given a string, count how many maximum-length palindromes are present.(It need not be a substring)
Input : str = "ababa" Output: 2 Explanation : palindromes of maximum of lenghts are : "ababa", "baaab" Input : str = "ababab" Output: 4 Explanation : palindromes of maximum of lenghts are : "ababa", "baaab", "abbba", "babab"
Approach A palindrome can be reprsented as “str + t + reverse(str)”.
Note: “t” is empty for even length palindromic strings
Calculate in how many ways “str” can be made and then multiply with “t” (number of single characters left out).
Let ci be the number of occurrences of character in the string. Consider the following cases:
1. If ci is even. Then a half of every maximum palindrome will contain exactly letters fi = ci / 2.
2.If ci is odd. Then a half of every maximum palindrome will contain exactly letters fi = (ci – 1)/ 2.
Let k be the number of odd ci. If k=0, the maximum palindromes length will be even; otherwise it will be odd and there will be exactly k possible middle letters i.e., we can set this letter to the middle of palindrome.
The number of permutations of n objects with n1 identical objects of type 1, n2 identical objects of type 2,, and n3 identical objects of type 3 is n! / (n1! * n2! * n3!) .
So here we have total number of characters as fa+fb+fa+…….+fy+fz . So number of permutation is (fa+fb+fa+…….+fy+fz)! / fa! fb!…fy!fz!.
Now If K is not 0, it’s obvious that the answer is k * (fa+fb+fa+…….+fy+fz+)! / fa! fb!…fy!fz!
Below is the implementation of the above.
Time Complexity : O(n)
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.
- Count special palindromes in a String
- Count of ways to split given string into two non-empty palindromes
- Check if suffix and prefix of a string are palindromes
- Split a string in equal parts such that all parts are palindromes
- Program to print all palindromes in a given range
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- 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
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count subsequences in first string which are anagrams of the second string
- Find the count of palindromic sub-string of a string in its sorted form
- Count occurrences of a string that can be constructed from another given string
- Count of times second string can be formed from the characters of first string
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Count words in a given string
- Given a binary string, count number of substrings that start and end with 1.
- Count All Palindromic Subsequence in a given String
- Count of distinct substrings of a string using Suffix Trie
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.