Two players are playing a game where a string str is given. The first player can take the characters at even indices and the second player can take the characters at odd indices. The player which can build the lexicographically smaller string than the other player wins the game. Print the winner of the game, either player A, B or print Tie if its a tie.
Input: str = “geeksforgeeks”
“eeggoss” is the lexicographically smallest
string that player A can get.
“eefkkr” is the lexicographically smallest
string that player B can get.
And B’s string is lexicographically smaller.
Input: str = “abcdbh”
Approach: Create two empty strings str1 and str2 for player A and B respectively. Traverse the original string character by character and for every character whose index is even, append this character in str1 else append this character in str2. Finally sort the generated string in order to get the lexicographically smallest possible string and compare them to find the winner of the game.
Below is the implementation of the above appoach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the winner of a game where scores are given as a binary string
- Dictionary and counter in Python to find winner of election
- Find winner of an election where votes are represented as candidate names
- Program to implement FLAMES game
- Find the Number which contain the digit d
- Find sub-string with given power
- Find whether X exists in Y after jumbling X
- Find the missing value from the given equation a + b = c
- Find the direction from given string
- Find substrings that contain all vowels
- Find strings that end with a given suffix
- Program to find the initials of a name.
- Find the most valued alphabet in the String
- Find the Substring with maximum product
- Find if an array contains a string with one mismatch
- Find the first repeated word in a string
- Find indices of all occurrence of one string in other
- Find n-th lexicographically permutation of a string | Set 2
- Find the first repeated character in a string
- Program to find second most frequent character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.