Make a palindromic string from given string | Function to find winner of the game ; Array to maintain frequency of the characters in S ; Initialise freq array with 0 ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driver Code ; Function call
function palindromeWinner ( S ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { freq [ i ] = 0 ; } let count = 0 ; for ( let i = 0 ; i < S . length ; ++ i ) { if ( freq [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) count ++ ; freq [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let unique = 0 ; let duplicate = 0 ; for ( let i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } let S = " " ; document . write ( " " + palindromeWinner ( S ) ) ;