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:
- 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
#include <bits/stdc++.h>
using namespace std;
void findPlayer(string str[], int n)
{
int move_first = 0;
int move_sec = 0;
for ( int i = 0; i < n; i++) {
if (str[i][0]
== str[i][str[i].length() - 1]) {
if (str[i][0] == 48)
move_first++;
else
move_sec++;
}
}
if (move_first <= move_sec) {
cout << "Player 2 wins" ;
}
else {
cout << "Player 1 wins" ;
}
}
int main()
{
string str[] = { "010" , "101" };
int N = sizeof (str)
/ sizeof (str[0]);
findPlayer(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findPlayer(String str[],
int n)
{
int move_first = 0 ;
int move_sec = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (str[i].charAt( 0 ) ==
str[i].charAt(str[i].length() - 1 ))
{
if (str[i].charAt( 0 ) == 48 )
move_first++;
else
move_sec++;
}
}
if (move_first <= move_sec)
{
System.out.print( "Player 2 wins" );
}
else
{
System.out.print( "Player 1 wins" );
}
}
public static void main(String[] args)
{
String str[] = { "010" , "101" };
int N = str[ 0 ].length();
findPlayer(str, N);
}
}
|
Python3
def findPlayer( str , n):
move_first = 0
move_sec = 0
for i in range (n):
if ( str [i][ 0 ] = =
str [i][ len ( str [i]) - 1 ]):
if ( str [i][ 0 ] = = 48 ):
move_first + = 1
else :
move_sec + = 1
if (move_first < = move_sec):
print ( "Player 2 wins" )
else :
print ( "Player 1 wins" )
str = [ "010" , "101" ]
N = len ( str )
findPlayer( str , N)
|
C#
using System;
class GFG{
static void findPlayer( string [] str, int n)
{
int move_first = 0;
int move_sec = 0;
for ( int i = 0; i < n; i++)
{
if (str[i][0] ==
str[i][str[i].Length - 1])
{
if ((str[i][0]) == 48)
move_first++;
else
move_sec++;
}
}
if (move_first <= move_sec)
{
Console.Write( "Player 2 wins" );
}
else
{
Console.Write( "Player 1 wins" );
}
}
public static void Main ()
{
string [] str = { "010" , "101" };
int N = str.Length;
findPlayer(str, N);
}
}
|
Javascript
<script>
function findPlayer(str, n)
{
let move_first = 0;
let move_sec = 0;
for (let i = 0; i < n - 1; i++)
{
if (str[i][0] ==
str[i][str[i].length - 1])
{
if (str[i][0]== 48)
move_first++;
else
move_sec++;
}
}
if (move_first <= move_sec)
{
document.write( "Player 2 wins" );
}
else
{
document.write( "Player 1 wins" );
}
}
let str = [ "010" , "101" ];
let N = str[0].length;
findPlayer(str, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...