Given a string str, find count of all palindromic permutations of it.
Input : str = "gfgf" Output : 2 There are two palindromic permutations fggf and gffg Input : str = "abc" Output : 0
The idea is based on below facts :
- A string can permute to a palindrome if number of odd occurring characters are at most one.
- One occurrence of the only odd character always goes to middle.
- Half of counts of all characters decide the result. In case of odd occurring character it is floor of half. Other half is automatically decided
For example if input string is “aabbccd”, the count of palindromic permutations is 3! (We get three by taking floor of half of all counts)
What if half itself has repeated characters?
We apply simple combinatorial rule and divide by factorial of half.
For example “aaaaaabbbb”, floor of half of string is 5. In half of a palindromic string, ‘a’ is repeated three times and ‘b’ is repeated two times, so our result is (5!) / (2!) * (3!).
The above solution causes overflow very early. We can avoid overflow by doing modular arithmetic. In the next article, we would be discussing modular arithmetic based approach.
- Print all the palindromic permutations of given string in alphabetic order
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Make palindromic string non-palindromic by rearranging its letters
- Count the number of special permutations
- Generate all cyclic permutations of a number
- Find the number of good permutations
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Palindromic divisors of a number
- Number of palindromic paths in a matrix
- Maximize the number of palindromic Strings
- Number of palindromic subsequences of length k where k <= 3
- Largest palindromic number in an array
- Next higher palindromic number using the same set of digits
- Number of Permutations such that no Three Terms forms Increasing Subsequence
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.