Number of palindromic permutations | Set 1
Given string str, find count of all palindromic permutations of it.
Examples :
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 is 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. The 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 a 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!).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 256;
long long int fact( int n)
{
long long int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
int countPalinPermutations(string &str)
{
int n = str.length();
int freq[MAX] = { 0 };
for ( int i = 0; i < n; i++)
freq[str[i]]++;
long long int res = fact(n / 2);
bool oddFreq = false ;
for ( int i = 0; i < MAX; i++) {
int half = freq[i] / 2;
if (freq[i] % 2 != 0) {
if (oddFreq == true )
return 0;
oddFreq = true ;
}
res = res / fact(half);
}
return res;
}
int main()
{
string str = "gffg" ;
cout << countPalinPermutations(str);
return 0;
}
|
Java
class GFG {
static final int MAX = 256 ;
static long fact( int n)
{
long res = 1 ;
for ( int i = 2 ; i <= n; i++)
res = res * i;
return res;
}
static int countPalinPermutations(String str)
{
int n = str.length();
int freq[]= new int [MAX];
for ( int i = 0 ; i < n; i++)
freq[str.charAt(i)]++;
long res = fact(n / 2 );
boolean oddFreq = false ;
for ( int i = 0 ; i < MAX; i++) {
int half = freq[i] / 2 ;
if (freq[i] % 2 != 0 ) {
if (oddFreq == true )
return 0 ;
oddFreq = true ;
}
res = res / fact(half);
}
return ( int )res;
}
public static void main (String[] args)
{
String str = "gffg" ;
System.out.print(
countPalinPermutations(str));
}
}
|
Python3
MAX = 256
def fact(n) :
res = 1
for i in range ( 2 , n + 1 ) :
res = res * i
return res
def countPalinPermutations( str ) :
global MAX
n = len ( str )
freq = [ 0 ] * MAX ;
for i in range ( 0 , n) :
freq[ ord ( str [i])] = freq[ ord ( str [i])] + 1 ;
res = fact( int (n / 2 ))
oddFreq = False
for i in range ( 0 , MAX ) :
half = int (freq[i] / 2 )
if (freq[i] % 2 ! = 0 ):
if (oddFreq = = True ):
return 0
oddFreq = True
res = int (res / fact(half))
return res
str = "gffg"
print (countPalinPermutations( str ))
|
C#
using System;
class GFG {
static int MAX = 256;
static long fact( int n)
{
long res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
static int countPalinPermutations( string str)
{
int n = str.Length;
int []freq= new int [MAX];
for ( int i = 0; i < n; i++)
freq[str[i]]++;
long res = fact(n / 2);
bool oddFreq = false ;
for ( int i = 0; i < MAX; i++) {
int half = freq[i] / 2;
if (freq[i] % 2 != 0) {
if (oddFreq == true )
return 0;
oddFreq = true ;
}
res = res / fact(half);
}
return ( int )res;
}
public static void Main ()
{
string str = "gffg" ;
Console.WriteLine(
countPalinPermutations(str));
}
}
|
PHP
<?php
$MAX = 256;
function fact( $n )
{
$res = 1;
for ( $i = 2; $i <= $n ; $i ++)
$res = $res * $i ;
return $res ;
}
function countPalinPermutations(& $str )
{
global $MAX ;
$n = strlen ( $str );
$freq = (0);
for ( $i = 0; $i < $n ; $i ++)
$res = fact( $n / 2);
$oddFreq = false;
for ( $i = 0; $i < $MAX ; $i ++)
{
$half = $freq [ $i ] / 2;
if ( $freq [ $i ] % 2 != 0)
{
if ( $oddFreq == true)
return 0;
$oddFreq = true;
}
$res = $res / fact( $half );
}
return $res ;
}
$str = "gffg" ;
echo countPalinPermutations( $str );
?>
|
Javascript
<script>
let MAX = 256;
function fact(n)
{
let res = 1;
for (let i = 2; i <= n; i++)
res = res * i;
return res;
}
function countPalinPermutations(str)
{
let n = str.length;
let freq = new Array(MAX);
freq.fill(0);
for (let i = 0; i < n; i++)
freq[str[i].charCodeAt()]++;
let res = fact(n / 2);
let oddFreq = false ;
for (let i = 0; i < MAX; i++) {
let half = freq[i] / 2;
if (freq[i] % 2 != 0) {
if (oddFreq == true )
return 0;
oddFreq = true ;
}
res = res / fact(half);
}
return res;
}
let str = "gffg" ;
document.write(countPalinPermutations(str));
</script>
|
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.
Last Updated :
05 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...