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++
// C++ program to implement // the above approach Ā
#include <bits/stdc++.h> using namespace std; Ā
// Function to find the winner of a game of // repeatedly removing the first character // to empty a string void find_Winner(vector<string>& arr, int N) { Ā
Ā Ā Ā Ā // Store characters of each Ā Ā Ā Ā // string of the array arr[] Ā Ā Ā Ā vector<queue< char > > Q(N); Ā
Ā Ā Ā Ā // Stores count of strings in arr[] Ā Ā Ā Ā int M = arr.size(); Ā
Ā Ā Ā Ā // Traverse the array arr[] Ā Ā Ā Ā for ( int i = 0; i < M; i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores length of current string Ā Ā Ā Ā Ā Ā Ā Ā int len = arr[i].length(); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the string Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < len; j++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert arr[i][j] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Q[i].push(arr[i][j] - 1); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // 1st Player starts the game Ā Ā Ā Ā int player = 0; Ā
Ā Ā Ā Ā while (Q[player].size() > 0) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the player number Ā Ā Ā Ā Ā Ā Ā Ā // for the next turn Ā Ā Ā Ā Ā Ā Ā Ā int nextPlayer Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = Q[player].front() - '0' ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Remove 1st character of Ā Ā Ā Ā Ā Ā Ā Ā // current string Ā Ā Ā Ā Ā Ā Ā Ā Q[player].pop(); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update player number for Ā Ā Ā Ā Ā Ā Ā Ā // the next turn Ā Ā Ā Ā Ā Ā Ā Ā player = nextPlayer; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā cout << "Player " << (player + 1); } Ā
// Driver Code int main() { Ā
Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā vector<string> arr Ā Ā Ā Ā Ā Ā Ā Ā = { "323" , "2" , "2" }; Ā
Ā Ā Ā Ā find_Winner(arr, N); Ā Ā Ā Ā return 0; } |
Java
// Java program to implement // the above approach import java.util.*; Ā
class GFG { Ā
// Function to find the winner of a game of // repeatedly removing the first character // to empty a String static void find_Winner(String[] arr, int N) { Ā
Ā Ā Ā Ā // Store characters of each Ā Ā Ā Ā // String of the array arr[] Ā Ā Ā Ā @SuppressWarnings ( "unchecked" ) Ā Ā Ā Ā Vector<Character> [] Q = new Vector[N]; Ā Ā Ā Ā for ( int i = 0 ; i < Q.length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Q[i] = new Vector<Character>(); Ā Ā Ā Ā Ā Ā Ā // Stores count of Strings in arr[] Ā Ā Ā Ā int M = arr.length; Ā
Ā Ā Ā Ā // Traverse the array arr[] Ā Ā Ā Ā for ( int i = 0 ; i < M; i++) Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores length of current String Ā Ā Ā Ā Ā Ā Ā Ā int len = arr[i].length(); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the String Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0 ; j < len; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert arr[i][j] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Q[i].add(arr[i].charAt(j)); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // 1st Player starts the game Ā Ā Ā Ā int player = 0 ; Ā
Ā Ā Ā Ā while (Q[player].size() > 0 ) Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the player number Ā Ā Ā Ā Ā Ā Ā Ā // for the next turn Ā Ā Ā Ā Ā Ā Ā Ā int nextPlayer Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = Q[player].get( 0 ) - '0' - 1 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Remove 1st character of Ā Ā Ā Ā Ā Ā Ā Ā // current String Ā Ā Ā Ā Ā Ā Ā Ā Q[player].remove( 0 ); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update player number for Ā Ā Ā Ā Ā Ā Ā Ā // the next turn Ā Ā Ā Ā Ā Ā Ā Ā player = nextPlayer; Ā Ā Ā Ā } Ā Ā Ā Ā System.out.print( "Player " +Ā (player + 1 )); } Ā
// Driver Code public static void main(String[] args) { Ā Ā Ā Ā int N = 3 ; Ā Ā Ā Ā String[] arr Ā Ā Ā Ā Ā Ā Ā Ā = { "323" , "2" , "2" }; Ā Ā Ā Ā find_Winner(arr, N); } } Ā
// This code is contributed by gauravrajput1 |
Python3
# Python3 program to implement # the above approach Ā
# Function to find the winner of a game of # repeatedly removing the first character # to empty a string def find_Winner(arr, N) : Ā Ā Ā Ā Ā Ā # Store characters of each Ā Ā Ā Ā # string of the array arr[] Ā Ā Ā Ā Q = [ 0 ] * NĀ Ā Ā Ā Ā Ā for i in range (N) :Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Q[i] = [] Ā Ā Ā Ā Ā Ā # Stores count of strings in arr[] Ā Ā Ā Ā M = len (arr) Ā Ā Ā Ā Ā Ā # Traverse the array arr[] Ā Ā Ā Ā for i in range (M) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Stores length of current string Ā Ā Ā Ā Ā Ā Ā Ā Len = len (arr[i]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Traverse the string Ā Ā Ā Ā Ā Ā Ā Ā for j in range ( Len ) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Insert arr[i][j] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Q[i].append( ord (arr[i][j]) - 1 ) Ā Ā Ā Ā Ā Ā # 1st Player starts the game Ā Ā Ā Ā player = 0 Ā Ā Ā Ā Ā Ā while ( len (Q[player]) > 0 ) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Stores the player number Ā Ā Ā Ā Ā Ā Ā Ā # for the next turn Ā Ā Ā Ā Ā Ā Ā Ā nextPlayer = Q[player][ 0 ] - ord ( '0' ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Remove 1st character of Ā Ā Ā Ā Ā Ā Ā Ā # current string Ā Ā Ā Ā Ā Ā Ā Ā del Q[player][ 0 ] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update player number for Ā Ā Ā Ā Ā Ā Ā Ā # the next turn Ā Ā Ā Ā Ā Ā Ā Ā player = nextPlayer Ā Ā Ā Ā Ā Ā print ( "Player" , (player + 1 )) Ā Ā Ā Ā Ā N = 3 arr = [ "323" , "2" , "2" ] Ā
find_Winner(arr, N) Ā
# This code is contributed by divyeshrabadiya07. |
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; Ā
class GFG{ Ā
// Function to find the winner of a game of // repeatedly removing the first character // to empty a String static void find_Winner(String[] arr, int N) { Ā Ā Ā Ā Ā Ā Ā Ā Ā // Store characters of each Ā Ā Ā Ā // String of the array []arr Ā Ā Ā Ā List< char > [] Q = new List< char >[N]; Ā Ā Ā Ā for ( int i = 0; i < Q.Length; i++) Ā Ā Ā Ā Ā Ā Ā Ā Q[i] = new List< char >(); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores count of Strings in []arr Ā Ā Ā Ā int M = arr.Length; Ā
Ā Ā Ā Ā // Traverse the array []arr Ā Ā Ā Ā for ( int i = 0; i < M; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores length of current String Ā Ā Ā Ā Ā Ā Ā Ā int len = arr[i].Length; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the String Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < len; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert arr[i,j] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Q[i].Add(arr[i][j]); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // 1st Player starts the game Ā Ā Ā Ā int player = 0; Ā
Ā Ā Ā Ā while (Q[player].Count > 0 ) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Stores the player number Ā Ā Ā Ā Ā Ā Ā Ā // for the next turn Ā Ā Ā Ā Ā Ā Ā Ā int nextPlayer = Q[player][0] - '0' - 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Remove 1st character of Ā Ā Ā Ā Ā Ā Ā Ā // current String Ā Ā Ā Ā Ā Ā Ā Ā Q[player].RemoveAt(0); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update player number for Ā Ā Ā Ā Ā Ā Ā Ā // the next turn Ā Ā Ā Ā Ā Ā Ā Ā player = nextPlayer; Ā Ā Ā Ā } Ā Ā Ā Ā Console.Write( "Player " + (player + 1)); } Ā
// Driver Code public static void Main(String[] args) { Ā Ā Ā Ā int N = 3; Ā Ā Ā Ā String[] arr = { "323" , "2" , "2" }; Ā Ā Ā Ā Ā Ā Ā Ā Ā find_Winner(arr, N); } } Ā
// This code is contributed by Rajput-Ji |
Javascript
<script> Ā
// Javascript program to implement // the above approach Ā
// Function to find the winner of a game of // repeatedly removing the first character // to empty a string function find_Winner( arr, N) { Ā
Ā Ā Ā Ā // Store characters of each Ā Ā Ā Ā // string of the array arr[] Ā Ā Ā Ā var Q = Array.from(Array(N), ()=>Array()) Ā
Ā Ā Ā Ā // Stores count of strings in arr[] Ā Ā Ā Ā var M = arr.length; Ā
Ā Ā Ā Ā // Traverse the array arr[] Ā Ā Ā Ā for ( var i = 0; i < M; i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores length of current string Ā Ā Ā Ā Ā Ā Ā Ā var len = arr[i].length; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the string Ā Ā Ā Ā Ā Ā Ā Ā for ( var j = 0; j < len; j++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Insert arr[i][j] Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Q[i].push(arr[i][j] - 1); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // 1st Player starts the game Ā Ā Ā Ā var player = 0; Ā
Ā Ā Ā Ā while (Q[player].length > 0) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the player number Ā Ā Ā Ā Ā Ā Ā Ā // for the next turn Ā Ā Ā Ā Ā Ā Ā Ā var nextPlayer Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = Q[player][0] - '0' ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Remove 1st character of Ā Ā Ā Ā Ā Ā Ā Ā // current string Ā Ā Ā Ā Ā Ā Ā Ā Q[player].shift(); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Update player number for Ā Ā Ā Ā Ā Ā Ā Ā // the next turn Ā Ā Ā Ā Ā Ā Ā Ā player = nextPlayer; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā document.write( "Player " + (player + 1)); } Ā
// Driver Code var N = 3; var arr Ā Ā Ā Ā = [ "323" , "2" , "2" ]; find_Winner(arr, N); Ā
// This code is contributed by rutvik_56. </script> |
Player 2
Ā
Time Complexity: O(N * M), where M is the length of the longest string present in the array.
Auxiliary Space: O(N * M)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!