String Game

Playerand Playerdecided to play a game. The game comprises of a String S which consist of lowercase English alphabets only and both players take alternative terms.

In each turn, a Player choose a character present in the string and remove all occurrences of the character. For each player to play his turn, there should be at least one character in the string. The Player who is not able to play his turn loses.

Your task is to find the winner of the game if both the players play optimally and Player1 plays the first turn.

Example:

Input:  s = "aba"
Output: Player2

Approach

C++

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

void stringGame(string s)
{
    int n = s.size();
    set<charst;
    for (int i = 0i < ni++)
        st.insert(s[i]);
    int len = st.size();
    if (len & 1)
        cout << "Player1\n";
    else
        cout << "Player2\n";
}
int main()
{

    string s = "aba";

    stringGame(s);

    return 0;
}


No comments:

Post a Comment