Find the winner of game of repeatedly removing the first character to empty given string


In this game, we are given an array of strings of length N. Each string consists of the digits 1 to N only. The game starts with the first person and removes the first character of the 0th index, then the removed character from the string digit number player will go for the next move. Each player with index y will remove the digit from the string from index y-1 and then the remove digit number player will move next. When any player is not able to remove the character will win the game.

Example

Let's understand the problem with the help of an input output example −

Input

string arr[] = {"323", "2", "2"}

Output

Player 2

Explanation

First player 1 will remove character 3, then player 3 will remove character 2, then player 2 will remove character 2 and again it will be its turn, but now all the elements of the second string are removed it will be the winner of the game.

Approach 1

In this approach, we are going to add all the string characters in the queues for each of the strings because this will make popping the first character easy.

We will maintain a queue vector for each player and start from the first player until the queue of any player will become empty using a while loop.

In each iteration we will pop the first character and store it in new character because this will be going to be our next player.

Example

#include <bits/stdc++.h>
using namespace std;

// function to get the winner of the game 
int getWinner(vector<string>& arr){
   int len = arr.size(); // maximum number of players 
   // storing each string character to the queue to make poping first character easy 
   // also convert character to an integer to make accessing easy 
   vector<queue<int>> q(len);
   // using for loop, traversing over the array 
   for(int i = 0; i < len; i++){
      // get the current string length 
      int cur_len = arr[i].size();
      // using nested for loop traverse over the current string 
      for (int j = 0; j < cur_len; j++){
         // push current character to the string by making it integer 
         q[i].push(arr[i][j] - '1');
      }
   }
   // starting with the first player
   int cur_player = 0;
   // using while loop traversing over the vector of queue 
   while(q[cur_player].empty() == false){
      // get the next player 
      int nextPlayer = q[cur_player].front();
      // remove current player 
      q[cur_player].pop();
      // update player to next player
      cur_player = nextPlayer;
   }
   // return the winner 
   return cur_player + 1;
}
int main(){
   int N = 3; // number of players
   vector<string> arr = { "323", "2", "2" }; // given array 
   // calling the function to get the winner 
   cout<<"The winner of the game is the player:" << getWinner(arr) << endl;
   return 0;
}

Output

The winner of the game is the player: 2

Time and Space Complexity

The time complexity of the above code is O(N*M), where N is the number of players or the size of the array and M is the size of the strings.

The space complexity is the same as the time complexity that is O(N*M), as we are using an extra queue vector to store the elements.

Approach 2

In this approach, we are going to replace queues with pointers only that will traverse to the next position instead of popping the last character, just move the pointer to the next position until any of the player's pointer reach the end of the respective end and wins. Let us see the code −

Example

#include <bits/stdc++.h>
using namespace std;
// function to get the winner of the game 
int getWinner(vector<string>& arr){
   int len = arr.size(); // maximum number of players 
   vector<queue<int>> q(len);
   // using for loop, traversing over the array 
   for(int i = 0; i < len; i++){
      // get the current string length 
      int cur_len = arr[i].size();
      // using nested for loop traverse over the current string 
      for (int j = 0; j < cur_len; j++){
         // push current character to the string by making it integer 
         q[i].push(arr[i][j] - '1');
      }
   }
   // starting with the first player
   int cur_player = 0;
   // using while loop traversing over the vector of queue 
   while(q[cur_player].empty() == false){
      int nextPlayer = q[cur_player].front(); // get the next player 
      q[cur_player].pop(); // remove current player 
      cur_player = nextPlayer;// update player to next player
   }
   return cur_player + 1; // return the winner 
}
int main(){
   int N = 3; // number of players
   vector<string> arr = { "323", "2", "2" }; // given array 
   cout<<"The winner of the game is the player:" << getWinner(arr) << endl;
   return 0;
}

Output

The winner of the game is the player: 2

Time and Space Complexity

The time complexity of the above code is O(N*M), where N is the number of players or the size of the array and M is the size of the strings.

The space complexity is O(N) as we are just using the one linear array of size N.

Conclusion

In this tutorial, we have implemented a program to find the winner of a game where we are given an array of size N and consists of the string that contains the number of characters in the range 1 to N. Starting from player 1 we will move to the player who is removed from the string of the previous player. We have implemented two programs with the same time complexity and one with the better space complexity.

Updated on: 24-Aug-2023

40 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements