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.
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 of K-size substrings having palindromic permutations
- 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
- Find the number of good permutations
- Count the number of special permutations
- Generate all cyclic permutations of a number
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Print all permutations of a number N greater than itself
- Generate all possible permutations of a Number divisible by N
- Smallest number containing all possible N length permutations using digits 0 to D
- Make palindromic string non-palindromic by rearranging its letters
- Find the Number of Permutations that satisfy the given condition in an array
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Maximize a number considering permutations with values smaller than limit
- Number of unique permutations starting with 1 of a Binary String
- Palindromic divisors of a number
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Number of palindromic paths in a matrix
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.