Determine the winner of a game of deleting Characters from a String | Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver code
function determineWinner ( str ) { let A = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; let sum1 = 0 , sum2 = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { A [ str [ i ] . charCodeAt ( ) - 48 ] ++ ; } for ( let i = 0 ; i <= 9 ; i ++ ) { if ( i % 2 != 0 ) { sum1 = sum1 + A [ i ] ; } else { sum2 = sum2 + A [ i ] ; } } if ( sum1 == sum2 ) { document . write ( " " ) ; } else if ( sum1 > sum2 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let str = " " ; determineWinner ( str ) ;