Given a binary string S, the task is to determine the winner of the game when two players play a game optimally in alternate turns with the given string, as per the following conditions:
- Player 1 always starts first.
- In each turn, a player removes a non-empty substring from the given string.
- After the given string is emptied, the player having the minimum count of 0s will win the game. If both players have equal count of 0s, then print “Tie”.
Examples:
Input: S = “00011”
Output: Player 1
Explanation: Substrings can be chosen as follows:
Turn 1: Player 1 removes the substring S[4…5]. Therefore, Player 1 contains “11”.
Turn 2: Player 2 removes the substring S[0…0]. Therefore, Player 2 contains “0”.
Turn 3: Player 1 removes the substring S[0…0]. Therefore, Player 1 contains “110”.
Turn 4: Player 2 removes the substring S[0…0]. Therefore, Player 2 contains “00”.
Therefore, Player 1 wins the game.Input: S = “0110011”
Output: Player 2
Approach: The problem can be solved based on the following observations:
- If count of 0s in the string is an even number then the player 1 and player 2 choose the substring “0” in each turn and no player will win this game.
- Otherwise, store the count of consecutive 1s in an array and apply the game of nim rule on the array.
- Nim-Sum: The cumulative XOR value of the number of coins/stones in each piles/heaps(here consecutive 1s) at any point of the game is called Nim-Sum at that point.
Follow the steps below to solve the problem:
- Initialize a variable, say cntZero, to store count of 0s in the string.
- Initialize a variable, say cntConOne, to store the count of consecutive 1s in the string.
- Initialize a variable, say nimSum, to store the Nim-Sum of consecutive 1s of the given string.
- Traverse the array and calculate count of 0s and nimSum.
- Finally, check if the value of cntZero is an even number or not. If found to be true, then print Tie.
- Otherwise, check if value of nimSum is greater than 0 or not. If found to be true, then print Player 1.
- Otherwise, print player 2.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the player // who wins the game void FindwinnerOfGame(string& S) { // Stores total count // of 0s in the string int cntZero = 0; // Stores count of // consecutive 1s int cntConOne = 0; // Stores Nim-Sum on count // of consecutive 1s int nimSum = 0; // Stores length // of the string int N = S.length(); // Traverse the string for ( int i = 0; i < N; i++) { // If the current // character is 1 if (S[i] == '1' ) { // Update cntConOne cntConOne += 1; } else { // Update nimSum nimSum ^= cntConOne; // Update cntConOne cntConOne = 0; // Update cntZero cntZero++; } } // Update nimSum nimSum ^= cntConOne; // If countZero is // an even number if (cntZero % 2 == 0) { cout << "Tie" ; } // nimSum is not 0 else if (nimSum) { cout << "player 1" ; } // If nimSum is zero else { cout << "player 2" ; } } // Driver Code int main() { string S = "0110011" ; FindwinnerOfGame(S); } |
Java
// Java program to implement // the above approach // Function to find the player // who wins the game class GFG { public static void FindwinnerOfGame(String S) { // Stores total count // of 0s in the string int cntZero = 0 ; // Stores count of // consecutive 1s int cntConOne = 0 ; // Stores Nim-Sum on count // of consecutive 1s int nimSum = 0 ; // Stores length // of the string int N = S.length(); // Traverse the string for ( int i = 0 ; i < N; i++) { // If the current // character is 1 if (S.charAt(i) == '1' ) { // Update cntConOne cntConOne += 1 ; } else { // Update nimSum nimSum ^= cntConOne; // Update cntConOne cntConOne = 0 ; // Update cntZero cntZero++; } } // Update nimSum nimSum ^= cntConOne; // If countZero is // an even number if (cntZero % 2 == 0 ) { System.out.print( "Tie" ); } // nimSum is not 0 else if (nimSum != 0 ) { System.out.print( "player 1" ); } // If nimSum is zero else { System.out.print( "player 2" ); } } // Driver Code public static void main(String[] args) { String S = "0110011" ; FindwinnerOfGame(S); } } // This code is contributed by grand_master. |
Python3
# Python 3 program to implement # the above approach # Function to find the player # who wins the game def FindwinnerOfGame(S): # Stores total count # of 0s in the string cntZero = 0 # Stores count of # consecutive 1s cntConOne = 0 # Stores Nim-Sum on count # of consecutive 1s nimSum = 0 # Stores length # of the string N = len (S) # Traverse the string for i in range (N): # If the current # character is 1 if (S[i] = = '1' ): # Update cntConOne cntConOne + = 1 else : # Update nimSum nimSum ^ = cntConOne # Update cntConOne cntConOne = 0 # Update cntZero cntZero + = 1 # Update nimSum nimSum ^ = cntConOne # If countZero is # an even number if (cntZero % 2 = = 0 ): print ( "Tie" ) # nimSum is not 0 elif (nimSum): print ( "player 1" ) # If nimSum is zero else : print ( "player 2" ) # Driver Code if __name__ = = '__main__' : S = "0110011" FindwinnerOfGame(S) # this code is contributed by SURENDRA_GANGWAR. |
C#
// C# program to implement // the above approach using System; // Function to find the player // who wins the game class GFG { public static void FindwinnerOfGame( string S) { // Stores total count // of 0s in the string int cntZero = 0; // Stores count of // consecutive 1s int cntConOne = 0; // Stores Nim-Sum on count // of consecutive 1s int nimSum = 0; // Stores length // of the string int N = S.Length; // Traverse the string for ( int i = 0; i < N; i++) { // If the current // character is 1 if (S[i] == '1' ) { // Update cntConOne cntConOne += 1; } else { // Update nimSum nimSum ^= cntConOne; // Update cntConOne cntConOne = 0; // Update cntZero cntZero++; } } // Update nimSum nimSum ^= cntConOne; // If countZero is // an even number if (cntZero % 2 == 0) { Console.Write( "Tie" ); } // nimSum is not 0 else if (nimSum != 0) { Console.Write( "player 1" ); } // If nimSum is zero else { Console.Write( "player 2" ); } } // Driver Code public static void Main( string [] args) { string S = "0110011" ; FindwinnerOfGame(S); } } // This code is contributed by ukasp. |
player 2
Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.