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
class GFG { public static void FindwinnerOfGame ( String S ) { int cntZero = 0 ; int cntConOne = 0 ; int nimSum = 0 ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { System . out . print ( " Tie " ) ; } else if ( nimSum != 0 ) { System . out . print ( " player ▁ 1" ) ; } else { System . out . print ( " player ▁ 2" ) ; } } public static void main ( String [ ] args ) { String S = "0110011" ; FindwinnerOfGame ( S ) ; } }