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 code
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 numberOfPossiblePallindrome ( 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 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 ( numberOfPossiblePallindrome ( str , n ) ) ; } }