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:
- 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.
- 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.
- If a string has a starting element different from its last element, it requires the player to make one extra move.
- So, just count the number of extra moves each player has to make.
- 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 gamevoid 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 Codeint 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 approachimport java.util.*;class GFG{ // Function to find the player who// loses the gamestatic 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 Codepublic 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 gamedef 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 stringsstr = [ "010", "101" ] N = len(str) # Function callfindPlayer(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 gamestatic 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 Codepublic 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 gamefunction 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> |
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


