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.
Examples:
Input: S = “abc”
Output: Player-2
Explanation:
There are all unique character due to which there
is no way to form a palindromic string of length > 1Input: S = “abccab”
Output: Player-2
Explanation:
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:
C++
// C++ Implementation to find // which player can form a palindromic // string first in a game #include <bits/stdc++.h> using namespace std; // Function to find // winner of the game int palindromeWinner(string& S) { // Array to Maintain frequency // of the characters in S int freq[26]; // Initialise freq array with 0 memset(freq, 0, sizeof freq); // Maintian count of all // distinct characters int count = 0; // Finding frequency of each character for (int i = 0; i < (int)S.length(); ++i) { if (freq[S[i] - 'a'] == 0) count++; freq[S[i] - 'a']++; } // Count unique duplicate // characters int unique = 0; int duplicate = 0; // Loop to count the unique // duplicate characters for (int i = 0; i < 26; ++i) { if (freq[i] == 1) unique++; else if (freq[i] >= 2) duplicate++; } // Condition for Player-1 // to be winner if (unique == 1 && (unique + duplicate) == count) return 1; // Else Player-2 is // always winner return 2; } // Driven Code int main() { string S = "abcbc"; // Function call cout << "Player-" << palindromeWinner(S) << endl; return 0; } |
Java
// Java implementation to find which // player can form a palindromic // string first in a game import java.util.*; class GFG{ // Function to find // winner of the game static int palindromeWinner(String S) { // Array to maintain frequency // of the characters in S int freq[] = new int[26]; // Initialise freq array with 0 Arrays.fill(freq, 0); // Maintian count of all // distinct characters int count = 0; // Finding frequency of each character for(int i = 0; i < (int)S.length(); ++i) { if (freq[S.charAt(i) - 'a'] == 0) count++; freq[S.charAt(i) - 'a']++; } // Count unique duplicate // characters int unique = 0; int duplicate = 0; // Loop to count the unique // duplicate characters for(int i = 0; i < 26; ++i) { if (freq[i] == 1) unique++; else if (freq[i] >= 2) duplicate++; } // Condition for Player-1 // to be winner if (unique == 1 && (unique + duplicate) == count) return 1; // Else Player-2 is // always winner return 2; } // Driver Code public static void main(String s[]) { String S = "abcbc"; // Function call System.out.println("Player-" + palindromeWinner(S)); } } // This code is contributed by rutvik_56 |
Python3
# Python3 implementation to find # which player can form a palindromic # string first in a game # Function to find # winner of the game def palindromeWinner(S): # Array to Maintain frequency # of the characters in S # initialise freq array with 0 freq = [0 for i in range(0, 26)] # Maintian count of all # distinct characters count = 0 # Finding frequency of each character for i in range(0, len(S)): if (freq[ord(S[i]) - 97] == 0): count += 1 freq[ord(S[i]) - 97] += 1 # Count unique duplicate # characters unique = 0 duplicate = 0 # Loop to count the unique # duplicate characters for i in range(0, 26): if (freq[i] == 1): unique += 1 elif (freq[i] >= 2): duplicate += 1 # Condition for Player-1 # to be winner if (unique == 1 and (unique + duplicate) == count): return 1 # Else Player-2 is # always winner return 2 # Driven Code S = "abcbc"; # Function call print("Player-", palindromeWinner(S)) # This code is contributed by Sanjit_Prasad |
Player-1
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.
Recommended Posts:
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum size substring to be removed to make a given string palindromic
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Minimum palindromic subarray removals to make array Empty
- Minimum minutes needed to make the time palindromic
- Find a palindromic string B such that given String A is a subsequense of B
- Find all distinct palindromic sub-strings of a given string
- Given a string, print all possible palindromic partitions
- Print all the palindromic permutations of given string in alphabetic order
- Find all palindromic sub-strings of a given string | Set 2
- Permutation of given string that maximizes count of Palindromic substrings
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Longest palindromic String formed using concatenation of given strings in any order
- Lexicographically all Shortest Palindromic Substrings from a given 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 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.

