Count maximum | factorial of a number ; function to count maximum length palindromes . ; Count number of occurrence of a charterer in the string ; let k = 0 ; Count of singles let num = 0 ; numerator of result let 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
function fact ( n ) { let ans = 1 ; for ( let i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ( ans ) ; } function numberOfPossiblePallindrome ( 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 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 = " " ; let n = str . length ; document . write ( numberOfPossiblePallindrome ( str , n ) ) ;