Skip to content
Related Articles

Related Articles

Improve Article
Find the player who is the last to remove any character from the beginning of a Binary String
  • Last Updated : 18 May, 2021

Given an array arr[] consisting of binary strings, the task is to find the winner of the game when two players play the game optimally as per the following rules: 

  • Player 1 starts the game.
  • In each turn, a player must choose a non-empty string and remove a positive number of characters from the beginning of the string.
  • Player 1 can only choose a string starting with the character ‘0’ whereas Player 2 can only choose a string starting with the character ‘1’.
  • A player who cannot make a move loses the game.

Examples: 

Input: arr[] = {“010”, “101”} 
Output: Player 2 
Explanation: 
First move for player 1 = {0, 101} 
First move for player 2 = {0, 1} 
Second move for player 1 = {1} 
Second move for player 2 = {} 
No moves left for player 1. 
Therefore player2 wins. 

Input: arr[] = {“010”, “001”} 
Output: Player 1 

Approach: The idea is to compare the total number of moves each player can make if both the players play the game optimally. Follow the steps below: 



  1. If there are consecutive occurrences of the same character in any string, then simply replace them with a single occurrence of that character, since it is optimal to remove all occurrences of the character present at the start.
  2. Now, if the string has a starting element same as its last element, then the scenario of the game remains the same even without this string because if one player makes a move on this string, the other player makes the next move by removing the character from the same string, resulting in the exact same position for the first player.
  3. If a string has a starting element different from its last element, it requires the player to make one extra move.
  4. So, just count the number of extra moves each player has to make.
  5. The player who runs out of extra moves will lose the game.

Below is the implementation of the above approach:

C++14




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the player who
// loses the game
void findPlayer(string str[], int n)
{
  
    // Moves for the first player
    int move_first = 0;
  
    // Moves for the second player
    int move_sec = 0;
  
    // Iterate over array of strings
    for (int i = 0; i < n; i++) {
  
        // Check if the first and last
        // character are the same
        if (str[i][0]
            == str[i][str[i].length() - 1]) {
  
            // Check if string start and
            // end with character '0'
            if (str[i][0] == 48)
                move_first++;
            else
                move_sec++;
        }
    }
  
    // If first player have less moves
    if (move_first <= move_sec) {
        cout << "Player 2 wins";
    }
    else {
        cout << "Player 1 wins";
    }
}
  
// Driver Code
int main()
{
    // Given array of strings
    string str[] = { "010", "101" };
  
    int N = sizeof(str)
            / sizeof(str[0]);
  
    // Function Call
    findPlayer(str, N);
  
    return 0;
}

Java




// Java program for
// the above approach
import java.util.*;
class GFG{
  
// Function to find the player who
// loses the game
static void findPlayer(String str[],
                       int n)
{
  // Moves for the
  // first player
  int move_first = 0;
 
  // Moves for the
  // second player
  int move_sec = 0;
 
  // Iterate over array
  // of Strings
  for (int i = 0; i < n - 1; i++)
  {
    // Check if the first and last
    // character are the same
    if (str[i].charAt(0) ==
        str[i].charAt(str[i].length() - 1))
    {
      // Check if String start and
      // end with character '0'
      if (str[i].charAt(0) == 48)
        move_first++;
      else
        move_sec++;
    }
  }
 
  // If first player have less moves
  if (move_first <= move_sec)
  {
    System.out.print("Player 2 wins");
  }
  else
  {
    System.out.print("Player 1 wins");
  }
}
  
// Driver Code
public static void main(String[] args)
{
  // Given array of Strings
  String str[] = {"010", "101"};
 
  int N = str[0].length();
   
  // Function Call
  findPlayer(str, N);
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python3 program for the above approach
  
# Function to find the player who
# loses the game
def findPlayer(str, n):
  
    # Moves for the first player
    move_first = 0
  
    # Moves for the second player
    move_sec = 0
  
    # Iterate over array of strings
    for i in range(n):
  
        # Check if the first and last
        # character are the same
        if (str[i][0] ==
            str[i][len(str[i]) - 1]):
  
            # Check if string start and
            # end with character '0'
            if (str[i][0] == 48):
                move_first += 1
            else:
                move_sec += 1
         
    # If first player have less moves
    if (move_first <= move_sec):
        print("Player 2 wins")
    else:
        print("Player 1 wins")
     
# Driver Code
 
# Given array of strings
str = [ "010", "101" ]
  
N = len(str)
  
# Function call
findPlayer(str, N)
 
# This code is contributed by sanjoy_62

C#




// C# program for the above approach 
using System;
 
class GFG{
  
// Function to find the player who
// loses the game
static void findPlayer(string[] str, int n)
{
     
    // Moves for the first player
    int move_first = 0;
  
    // Moves for the second player
    int move_sec = 0;
  
    // Iterate over array of strings
    for(int i = 0; i < n; i++)
    {
         
        // Check if the first and last
        // character are the same
        if (str[i][0] ==
            str[i][str[i].Length - 1])
        {
             
            // Check if string start and
            // end with character '0'
            if ((str[i][0]) == 48)
                move_first++;
            else
                move_sec++;
        }
    }
  
    // If first player have less moves
    if (move_first <= move_sec)
    {
        Console.Write("Player 2 wins");
    }
    else
    {
        Console.Write("Player 1 wins");
    }
}
  
// Driver Code
public static void Main ()
{
     
    // Given array of strings
    string[] str = { "010", "101" };
  
    int N = str.Length;
  
    // Function call
    findPlayer(str, N);
}
}
 
// This code is contributed by sanjoy_62

Javascript




<script>
// javascript program for the
// above approach
 
// Function to find the player who
// loses the game
function findPlayer(str, n)
{
  // Moves for the
  // first player
  let move_first = 0;
  
  // Moves for the
  // second player
  let move_sec = 0;
  
  // Iterate over array
  // of Strings
  for (let i = 0; i < n - 1; i++)
  {
    // Check if the first and last
    // character are the same
    if (str[i][0] ==
        str[i][str[i].length - 1])
    {
      // Check if String start and
      // end with character '0'
      if (str[i][0]== 48)
        move_first++;
      else
        move_sec++;
    }
  }
  
  // If first player have less moves
  if (move_first <= move_sec)
  {
    document.write("Player 2 wins");
  }
  else
  {
    document.write("Player 1 wins");
  }
}
 
  
// Driver Code
 
    // Given array of Strings
  let str = ["010", "101"];
  
  let N = str[0].length;
    
  // Function Call
  findPlayer(str, N);
           
</script>
Output: 
Player 2 wins

 

Time Complexity: O(N) 
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :