Given a string, count how many maximum-length palindromes are present. (It need not be a substring)
Examples:
Input : str = "ababa"
Output: 2
Explanation :
palindromes of maximum of lengths are :
"ababa", "baaab"
Input : str = "ababab"
Output: 4
Explanation :
palindromes of maximum of lengths are :
"ababa", "baaab", "abbba", "babab"
Approach A palindrome can be represented 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 a character in the string. Consider the following cases:
- If ci is even. Then half of every maximum palindrome will contain exactly letters fi = ci / 2.
- If ci is odd. Then 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 length of the maximum palindrome 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 the 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.
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
int ans = 1;
for ( int i = 1; i <= n; i++)
ans = ans * i;
return (ans);
}
int numberOfPossiblePalindrome(string str, int n)
{
unordered_map< char , int > mp;
for ( int i = 0; i < n; i++)
mp[str[i]]++;
int k = 0;
int num = 0;
int den = 1;
int fi;
for ( auto it = mp.begin(); it != mp.end(); ++it)
{
if (it->second % 2 == 0)
fi = it->second / 2;
else
{
fi = (it->second - 1) / 2;
k++;
}
num = num + fi;
den = den * fact(fi);
}
if (num != 0)
num = fact(num);
int ans = num / den;
if (k != 0)
{
ans = ans * k;
}
return (ans);
}
int main()
{
char str[] = "ababab" ;
int n = strlen (str);
cout << numberOfPossiblePalindrome(str, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int fact( int n)
{
int ans = 1 ;
for ( int i = 1 ; i <= n; i++)
ans = ans * i;
return (ans);
}
static int numberOfPossiblePalindrome(String str, int n)
{
Map<Character,Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
mp.put( str.charAt(i),mp.get( str.charAt(i))== null ?
1 :mp.get( str.charAt(i))+ 1 );
int k = 0 ;
int num = 0 ;
int den = 1 ;
int fi;
for (Map.Entry<Character,Integer> it : mp.entrySet())
{
if (it.getValue() % 2 == 0 )
fi = it.getValue() / 2 ;
else
{
fi = (it.getValue() - 1 ) / 2 ;
k++;
}
num = num + fi;
den = den * fact(fi);
}
if (num != 0 )
num = fact(num);
int ans = num / den;
if (k != 0 )
{
ans = ans * k;
}
return (ans);
}
public static void main(String[] args)
{
String str = "ababab" ;
int n = str.length();
System.out.println(numberOfPossiblePalindrome(str, n));
}
}
|
Python3
import math as mt
def fact(n):
ans = 1
for i in range ( 1 , n + 1 ):
ans = ans * i
return (ans)
def numberOfPossiblePalindrome(string, n):
mp = dict ()
for i in range (n):
if string[i] in mp.keys():
mp[string[i]] + = 1
else :
mp[string[i]] = 1
k = 0
num = 0
den = 1
fi = 0
for it in mp:
if (mp[it] % 2 = = 0 ):
fi = mp[it] / / 2
else :
fi = (mp[it] - 1 ) / / 2
k + = 1
num = num + fi
den = den * fact(fi)
if (num ! = 0 ):
num = fact(num)
ans = num / / den
if (k ! = 0 ):
ans = ans * k
return (ans)
string = "ababab"
n = len (string)
print (numberOfPossiblePalindrome(string, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int fact( int n)
{
int ans = 1;
for ( int i = 1; i <= n; i++)
ans = ans * i;
return (ans);
}
static int numberOfPossiblePalindrome(String str, int n)
{
Dictionary< char , int > mp = new Dictionary< char , int >();
for ( int i = 0 ; i < n; i++)
{
if (mp.ContainsKey(str[i]))
{
var val = mp[str[i]];
mp.Remove(str[i]);
mp.Add(str[i], val + 1);
}
else
{
mp.Add(str[i], 1);
}
}
int k = 0;
int num = 0;
int den = 1;
int fi;
foreach (KeyValuePair< char , int > it in mp)
{
if (it.Value % 2 == 0)
fi = it.Value / 2;
else
{
fi = (it.Value - 1) / 2;
k++;
}
num = num + fi;
den = den * fact(fi);
}
if (num != 0)
num = fact(num);
int ans = num / den;
if (k != 0)
{
ans = ans * k;
}
return (ans);
}
public static void Main(String[] args)
{
String str = "ababab" ;
int n = str.Length;
Console.WriteLine(numberOfPossiblePalindrome(str, n));
}
}
|
Javascript
<script>
function fact(n)
{
let ans = 1;
for (let i = 1; i <= n; i++)
ans = ans * i;
return (ans);
}
function numberOfPossiblePalindrome(str,n)
{
let mp = new Map();
for (let i = 0; i < n; i++)
mp.set( str[i],mp.get( str[i])== null ?
1:mp.get( str[i])+1);
let k = 0;
let num = 0;
let den = 1;
let fi;
for (let [key, value] of mp.entries())
{
if (value % 2 == 0)
fi = value / 2;
else
{
fi = (value - 1) / 2;
k++;
}
num = num + fi;
den = den * fact(fi);
}
if (num != 0)
num = fact(num);
let ans = Math.floor(num / den);
if (k != 0)
{
ans = ans * k;
}
return (ans);
}
let str = "ababab" ;
let n = str.length;
document.write(numberOfPossiblePalindrome(str, n));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Nov, 2022
Like Article
Save Article