Number of palindromic permutations


Permutations are possible in strings as well as numbers. A string can have permutations equal to the factorial of its number of characters. These permutations can be palindromic under certain circumstances.

In this article, we will discuss about how palindromic permutations occur in a string. We will also find the number of palindromic permutations possible in a string using C++.

Permutation is a mathematical process of rearranging the letters or characters from a specified string or words. In other words, it is rearrangement of objects or elements in an order. A palindrome is a set of character which are same when read from beginning or end.

We are given a string. For the string, we have to find the maximum possible palindromic permutations.

Input Output Scenarios

We are given the string. The number of palindromic permutations in that string is the output.

Input: string = "ababc"
Output: 2
Input: string = "xxyyyxxzzy"
Output: 30

Here, in case of string = "ababc", we have 2 palindromic permutations possible. They are abcba and bacab.

For a string to have palindromic permutations, it should have the following properties −

  • It should have even frequencies of characters in it.

  • In case of odd frequency of characters, there should be only one such character. For example, ababc has 2 ‘a’, 2 ‘b’ and 1 ‘c’.

For making palindromic sequences, we take the half number of characters from the string and make the possible permutations using them. Next, we reverse the order of characters for each outcome and append it with the outcomes obtained after permutations.

Using Factorial

We can find palindromic permutations in a string by doing the factorial of half of the frequency of characters. If there are repetition for the half’s, we divide by the factorial of repeated frequency of the half. For example, the half of the string “xxyyyxxzzy” is 5. After doing the half of the string, x and y are repeated 2 times. So, the number of palindromic permutations will be equal to (5!) / [(2!) * (2!)]. The result is 30.

Example

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Calculate the factorial
long long int factorial(int n) {
   long long int value = 1;
   for (int i = 2; i <= n; i++) {
      value *= i;
   }
   return value;
}
int totalCount(string str) {
   vector < int > num(256);
   for (char ch: str) {
      num[ch]++;
   }
   // Taking factorial of half number of characters
   long long int result = factorial(str.length() / 2);
   // Check for the odd frequency
   bool odd = false;
   // Finding the frequency of the characters
   for (int i = 0; i < 256; i++) {
      int half = num[i] / 2;
      if (num[i] % 2 != 0) {
         if (odd) {
            return 0;
         }
         odd = true;
      }
      result = result / factorial(half);
   }
   return result;
}
int main() {
   string str = "ababc";
   cout << "Number of palindromic permutations in the string " << str << " are " << totalCount(str);
   return 0;
}

Output

Number of palindromic permutations in the string ababc are 2

Using Frequency Counting

We find and store the frequency of each character of the string using unordered_map data structure. We then find whether there are any odd characters or not. Next, we use the factorial formula to get the total count of palindromic permutations in the string.

Example

#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
int totalCount(string str) {
   if (str.size() == 0) {
      return 0;
   }
   // store frequency of each character
   unordered_map<char, int> freq;
   for (char c: str) {
      freq[c]++;
   }
   // Finding the odd character (if present)
   int odd_value = 0;
   string mid;
   string start;
   for (auto itr: freq){
      char c = itr.first;
      int x = itr.second;
      if ((x & 1)){
         if (++odd_value > 1) {
            return 0;
         }
         x = x - 1;
         mid = itr.first;
      }
      // Append x/2 characters to other half
      x = x/2;
      while (x--) {
         start = start + c;
      }
   }
   int num = 1;
   int halfLength = start.size();
   for (int j = 2; j <= halfLength; j++) {
      num *= j;
   }
   return num;
}
int main(){
   string str = "ababc";
   cout << "Number of palindromic permutations in the string " << str << " are "
   << totalCount(str);
   return 0;
}

Output

Number of palindromic permutations in the string ababc are 2

Conclusion

We have discussed about different ways to find the number of palindromic permutations in a string. The first approach is a simple one using the factorial of the combinations. The second approach uses frequency counting. If we want to print all the possible outcomes, we can use the sort() function and while loop. We can print them in lexicographic order.

Updated on: 05-Jan-2024

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements