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 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<char> st;for (int i = 0; i < n; i++)st.insert(s[i]);int len = st.size();if (len & 1)cout << "Player1\n";elsecout << "Player2\n";}int main(){string s = "aba";stringGame(s);return 0;}
No comments:
Post a Comment