# Find the player who rearranges the characters to get a palindrome string first

Given an even length string S consisting of lower-case English alphabets only, we have two players playing the game. The rules are as follows:

• the player wins the game, if, at any move, a player can re-arrange the characters of the string to get a palindrome string.
• if the player cannot win the game, he has to remove any character from the string.

Both players play the game optimally with player-1 starting the game. The task is to print the winner of the game.

Examples:

Input: S = “abaaab”
Output: Player-1
Player-1 in the first step arranges the characters to get “aabbaa” and wins the game.

Input: S = “abca”
Output: Player-2
As the game is being played optimally, player-1 removes ‘a’ to get string “bca” which cannot be rearranged by player-2 in the second move to win the game.
Player-2 optimally removes ‘b’ and the string is now “ca”.
In the third move, player-1 removes “a” as he cannot rearrange the characters, the new string is “c”, which the player-2 at the next move can make a palindrome.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Count the frequencies of each character in a freq[] array.
• Count the characters that occur odd number of times.
• If the count is 0 or an odd number, then Player-1 will always win the game, else player-2 will win because player-2 will make the last move.

Below is the implementation of the above approach:

## C++

 `// C++ program to print the winner of the game ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns the winner of the game ` `int` `returnWinner(string s, ``int` `l) ` `{ ` `    ``// Initialize the freq array to 0 ` `    ``int` `freq; ` `    ``memset``(freq, 0, ``sizeof` `freq); ` ` `  `    ``// Iterate and count the frequencies ` `    ``// of each character in the string ` `    ``for` `(``int` `i = 0; i < l; i++) { ` `        ``freq[s[i] - ``'a'``]++; ` `    ``} ` ` `  `    ``int` `cnt = 0; ` ` `  `    ``// Count the odd occurring character ` `    ``for` `(``int` `i = 0; i < 26; i++) { ` ` `  `        ``// If odd occurrence ` `        ``if` `(freq[i] & 1) ` `            ``cnt++; ` `    ``} ` ` `  `    ``// Check condition for Player-1 winning the game ` `    ``if` `(cnt == 0 || cnt & 1) ` `        ``return` `1; ` `    ``else` `        ``return` `2; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"abaaab"``; ` `    ``int` `l = s.length(); ` ` `  `    ``// Function call that returns the winner ` `    ``int` `winner = returnWinner(s, l); ` ` `  `    ``cout << ``"Player-"` `<< winner; ` `    ``return` `0; ` `} `

## Java

 `// Java program to print the winner of the game  ` `class` `GfG { ` `// Function that returns the winner of the game  ` `static` `int` `returnWinner(String s, ``int` `l)  ` `{  ` `    ``// Initialize the freq array to 0  ` `    ``int` `freq[]  =``new` `int``[``26``];  ` ` `  `    ``// Iterate and count the frequencies  ` `    ``// of each character in the string  ` `    ``for` `(``int` `i = ``0``; i < l; i++) {  ` `        ``freq[s.charAt(i) - ``'a'``]++;  ` `    ``}  ` ` `  `    ``int` `cnt = ``0``;  ` ` `  `    ``// Count the odd occurring character  ` `    ``for` `(``int` `i = ``0``; i < ``26``; i++) {  ` ` `  `        ``// If odd occurrence  ` `        ``if` `(freq[i] % ``2` `!= ``0``)  ` `            ``cnt++;  ` `    ``}  ` ` `  `    ``// Check condition for Player-1 winning the game  ` `    ``if` `((cnt == ``0``)|| (cnt & ``1``) == ``1``)  ` `        ``return` `1``;  ` `    ``else` `        ``return` `2``;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``String s = ``"abaaab"``;  ` `    ``int` `l = s.length();  ` ` `  `    ``// Function call that returns the winner  ` `    ``int` `winner = returnWinner(s, l);  ` ` `  `    ``System.out.println(``"Player-"` `+ winner);  ` `} ` `}  `

## Python3

 `# Python 3 program to print the winner of the game ` ` `  `# Function that returns the winner of the game ` `def` `returnWinner(s, l): ` `     `  `    ``# Initialize the freq array to 0 ` `    ``freq ``=` `[``0` `for` `i ``in` `range``(``26``)] ` ` `  `    ``# Iterate and count the frequencies ` `    ``# of each character in the string ` `    ``for` `i ``in` `range``(``0``, l, ``1``): ` `        ``freq[``ord``(s[i]) ``-` `ord``(``'a'``)] ``+``=` `1` ` `  `    ``cnt ``=` `0` ` `  `    ``# Count the odd occurring character ` `    ``for` `i ``in` `range``(``26``): ` `         `  `        ``# If odd occurrence ` `        ``if` `(freq[i] ``%` `2` `!``=` `0``): ` `            ``cnt ``+``=` `1` ` `  `    ``# Check condition for Player-1  ` `    ``# winning the game ` `    ``if` `(cnt ``=``=` `0` `or` `cnt & ``1` `=``=` `1``): ` `        ``return` `1` `    ``else``: ` `        ``return` `2` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``s ``=` `"abaaab"` `    ``l ``=` `len``(s) ` ` `  `    ``# Function call that returns the winner ` `    ``winner ``=` `returnWinner(s, l) ` ` `  `    ``print``(``"Player-"``, winner) ` `     `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to print the winner of the game  ` `using` `System; ` ` `  `class` `GfG  ` `{ ` `     `  `// Function that returns the winner of the game  ` `static` `int` `returnWinner(String s, ``int` `l)  ` `{  ` `    ``// Initialize the freq array to 0  ` `    ``int` `[]freq = ``new` `int``;  ` ` `  `    ``// Iterate and count the frequencies  ` `    ``// of each character in the string  ` `    ``for` `(``int` `i = 0; i < l; i++)  ` `    ``{  ` `        ``freq[s[i] - ``'a'``]++;  ` `    ``}  ` ` `  `    ``int` `cnt = 0;  ` ` `  `    ``// Count the odd occurring character  ` `    ``for` `(``int` `i = 0; i < 26; i++)  ` `    ``{  ` ` `  `        ``// If odd occurrence  ` `        ``if` `(freq[i] % 2 != 0)  ` `            ``cnt++;  ` `    ``}  ` ` `  `    ``// Check condition for Player-1 winning the game  ` `    ``if` `((cnt == 0)|| (cnt & 1) == 1)  ` `        ``return` `1;  ` `    ``else` `        ``return` `2;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``String s = ``"abaaab"``;  ` `    ``int` `l = s.Length;  ` ` `  `    ``// Function call that returns the winner  ` `    ``int` `winner = returnWinner(s, l);  ` ` `  `    ``Console.WriteLine(``"Player-"` `+ winner);  ` `} ` `}  ` ` `  `// This code contributed by Rajput-Ji `

## PHP

 ` `

Output:

```Player-1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.