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.
Input: S = “abaaab”
Player-1 in the first step arranges the characters to get “aabbaa” and wins the game.
Input: S = “abca”
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.
- 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:
- Check if characters of a given string can be rearranged to form a palindrome
- Minimum characters to be added at front to make string palindrome
- Minimum number of characters to be replaced to make a given string Palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Find the smallest window in a string containing all characters of another string
- Find longest palindrome formed by removing or shuffling chars from string
- Python Dictionary to find mirror characters in a string
- Find the longest substring with k unique characters in a given string
- Program to find the product of ASCII values of characters in a string
- Find largest word in dictionary by deleting some characters of given string
- Program to find the largest and smallest ASCII valued characters in a string
- Next word that does not contain a palindrome and has characters from first k
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange characters to form palindrome if possible
- Min flips of continuous characters to make all characters same in a string