Find the player with least 0 s after emptying a Binary String by removing non | Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code
function FindwinnerOfGame ( S ) { let cntZero = 0 ; let cntConOne = 0 ; let nimSum = 0 ; let N = S . length ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { document . write ( " " ) ; } else if ( nimSum != 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let S = " " ; FindwinnerOfGame ( S ) ;