Count maximum | C ++ implementation for counting maximum length palindromes ; factorial of a number ; function to count maximum length palindromes . ; Count number of occurrence of a charterer in the string ; int k = 0 ; Count of singles int num = 0 ; numerator of result int den = 1 ; denominator of result ; if frequency is even fi = ci / 2 ; if frequency is odd fi = ci - 1 / 2. ; sum of all frequencies ; product of factorial of every frequency ; if all character are unique so there will be no pallindrome , so if num != 0 then only we are finding the factorial ; k are the single elements that can be placed in middle ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ( ans ) ; } int numberOfPossiblePallindrome ( string str , int n ) { unordered_map < char , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ str [ i ] ] ++ ; 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 << numberOfPossiblePallindrome ( str , n ) ; return 0 ; }