Open In App

Find the player with least 0s after emptying a Binary String by removing non-empty substrings

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:

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:

Follow the steps below to solve the problem:

Below is the implementation of the above approach: 




// 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 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.




# 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# 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.




<script>
// javascript program of the above approach
 
    // Function to find the player
// who wins the game
    function FindwinnerOfGame(S)
    {
 
        // Stores total count
        // of 0s in the string
        let cntZero = 0;
 
        // Stores count of
        // consecutive 1s
        let cntConOne = 0;
 
        // Stores Nim-Sum on count
        // of consecutive 1s
        let nimSum = 0;
 
        // Stores length
        // of the string
        let N = S.length;
 
        // Traverse the string
        for (let 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) {
            document.write("Tie");
        }
 
        // nimSum is not 0
        else if (nimSum != 0) {
            document.write("player 1");
        }
 
        // If nimSum is zero
        else {
            document.write("player 2");
        }
    }
 
    // Driver Code
     
           let S = "0110011";
        FindwinnerOfGame(S);
 
</script>

Output: 
player 2

 

Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(1)


Article Tags :