Find the winner of the game | Function to find the winner of the game ; To store the strings for both the players ; If the index is even ; Append the current character to player A 's string ; If the index is odd ; Append the current character to player B 's string ; Sort both the strings to get the lexicographically smallest string possible ; Copmpare both the strings to find the winner of the game ; Driver code
function find_winner ( str , n ) { var str1 = " " , str2 = " " ; for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { str1 += str [ i ] ; } else { str2 += str [ i ] ; } } str1 = str1 . split ( ' ' ) . sort ( ) ; str2 = str2 . split ( ' ' ) . sort ( ) ; if ( str1 < str2 ) document . write ( " " ) ; else if ( str2 < str1 ) document . write ( " " ) ; else document . write ( " " ) ; } var str = " " ; var n = str . length ; find_winner ( str , n ) ;