Find the winner of game of repeatedly removing the first character to empty given string
Last Updated :
09 Jun, 2021
Given a positive integer N, representing the count of players playing the game and an array of strings arr[], consisting of the numeric strings made up of digits from the range [‘1’, ‘N’]. Considering ith player is assigned with the string arr[i], the task is to find the winner of the game when all N players play the game optimally as per the following rules:
- Player 1 starts the game, removes the first character of the string arr[1]( 1-based indexing ), say X, and in the next turn Xth player will play the game and remove the first character of arr[X] and so on.
- The player who is not able to remove any character from the assigned string will win the game.
Examples:
Input: N = 3, arr[] = { “323”, “2”, “2” }
Output: Player 2
Explanation:
Turn 1: Removing arr[0][0] by Player 1 modifies arr[0] to “23”.
Turn 2: Removing arr[2][0] by Player 3 modifies arr[2] to “”.
Turn 3: Removing arr[1][0] by Player 2 modifies arr[1] to “”.
Turn 4: Player 2 is not able to remove any characters from arr[1].
Therefore, player 2 wins the game.
Input: N = 3, arr[] = { “121”, “21”, “23123” }
Output: Player 1
Approach: The problem can be solved using Queue to remove the first character of each string efficiently. Follow the steps below to solve the problem:
- Initialize an array of Queues, say Q[], such that Q[i] stores the characters of the string arr[i].
- Traverse the array Q[] using variable i as per the rules of the game and check if the count of characters in Q[i] is 0 or not. If found to be true, then print the “Player i”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_Winner(vector<string>& arr, int N)
{
vector<queue< char > > Q(N);
int M = arr.size();
for ( int i = 0; i < M; i++) {
int len = arr[i].length();
for ( int j = 0; j < len; j++) {
Q[i].push(arr[i][j] - 1);
}
}
int player = 0;
while (Q[player].size() > 0) {
int nextPlayer
= Q[player].front() - '0' ;
Q[player].pop();
player = nextPlayer;
}
cout << "Player " << (player + 1);
}
int main()
{
int N = 3;
vector<string> arr
= { "323" , "2" , "2" };
find_Winner(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void find_Winner(String[] arr, int N)
{
@SuppressWarnings ( "unchecked" )
Vector<Character> [] Q = new Vector[N];
for ( int i = 0 ; i < Q.length; i++)
Q[i] = new Vector<Character>();
int M = arr.length;
for ( int i = 0 ; i < M; i++)
{
int len = arr[i].length();
for ( int j = 0 ; j < len; j++)
{
Q[i].add(arr[i].charAt(j));
}
}
int player = 0 ;
while (Q[player].size() > 0 )
{
int nextPlayer
= Q[player].get( 0 ) - '0' - 1 ;
Q[player].remove( 0 );
player = nextPlayer;
}
System.out.print( "Player " + (player + 1 ));
}
public static void main(String[] args)
{
int N = 3 ;
String[] arr
= { "323" , "2" , "2" };
find_Winner(arr, N);
}
}
|
Python3
def find_Winner(arr, N) :
Q = [ 0 ] * N
for i in range (N) :
Q[i] = []
M = len (arr)
for i in range (M) :
Len = len (arr[i])
for j in range ( Len ) :
Q[i].append( ord (arr[i][j]) - 1 )
player = 0
while ( len (Q[player]) > 0 ) :
nextPlayer = Q[player][ 0 ] - ord ( '0' )
del Q[player][ 0 ]
player = nextPlayer
print ( "Player" , (player + 1 ))
N = 3
arr = [ "323" , "2" , "2" ]
find_Winner(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void find_Winner(String[] arr, int N)
{
List< char > [] Q = new List< char >[N];
for ( int i = 0; i < Q.Length; i++)
Q[i] = new List< char >();
int M = arr.Length;
for ( int i = 0; i < M; i++)
{
int len = arr[i].Length;
for ( int j = 0; j < len; j++)
{
Q[i].Add(arr[i][j]);
}
}
int player = 0;
while (Q[player].Count > 0 )
{
int nextPlayer = Q[player][0] - '0' - 1;
Q[player].RemoveAt(0);
player = nextPlayer;
}
Console.Write( "Player " + (player + 1));
}
public static void Main(String[] args)
{
int N = 3;
String[] arr = { "323" , "2" , "2" };
find_Winner(arr, N);
}
}
|
Javascript
<script>
function find_Winner( arr, N)
{
var Q = Array.from(Array(N), ()=>Array())
var M = arr.length;
for ( var i = 0; i < M; i++) {
var len = arr[i].length;
for ( var j = 0; j < len; j++) {
Q[i].push(arr[i][j] - 1);
}
}
var player = 0;
while (Q[player].length > 0) {
var nextPlayer
= Q[player][0] - '0' ;
Q[player].shift();
player = nextPlayer;
}
document.write( "Player " + (player + 1));
}
var N = 3;
var arr
= [ "323" , "2" , "2" ];
find_Winner(arr, N);
</script>
|
Time Complexity: O(N * M), where M is the length of the longest string present in the array.
Auxiliary Space: O(N * M)
Share your thoughts in the comments
Please Login to comment...