Given a string S consisting of lower-case English alphabets only, we have two players playing the game. The rules are as follows:
Both plays optimally with player-1 starting the game. The task is to find the winner of the game.
Input: S = “abc”
There are all unique character due to which there
is no way to form a palindromic string of length > 1
Input: S = “abccab”
Initially newString = “” is empty.
Let Player-1 chooses character ‘a’ and write it on paper.
Then, S = “bccab” and newString = “a”.
Now Player-2 chooses character ‘a’ from S and write it on left side of newString.
Thus, S = “bccb” and newString = “aa”.
Now, newString = “aa” is a palindrome of length 2.
Hence Player-2 wins.
Approach : The idea is to formulate a condition in which Player-1 is always going to be the winner. If the condition fails, then Player-2 will win the game.
- If there is only one unique character occurring once in the given string, and the rest of the characters occurring more than 1 then Player-1 is going to be the winner, else Player-2 will win always.
- If we have all characters that are occurring more than once in the given string, then Player-2 can always copy Player-1 move in his first turn and wins.
- Also, if we have more than one character in the string occurring one time only, then a palindrome string can never be formed(in optimal case), hence again Player-2 wins.
Below is the implementation of above approach:
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find the count of palindromic sub-string of a string in its sorted form
- Check if a string contains a palindromic sub-string of even length
- Find a palindromic string B such that given String A is a subsequense of B
- Minimum changes required to make first string substring of second string
- Check if a string can be repeated to make another string
- Lexicographically first palindromic string
- Find all palindromic sub-strings of a given string | Set 2
- Given a string, print all possible palindromic partitions
- Count palindromic characteristics of a String
- Print all palindromic partitions of a string
- Count All Palindromic Subsequence in a given String
- Print the longest palindromic prefix of a given string
- Find all distinct palindromic sub-strings of a given string
- Length of longest palindromic sub-string : Recursion
- Check if K palindromic strings can be formed from a given string
- Number of strings of length N with no palindromic sub string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.