# Find the winner of the game

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.

Examples:

Input: str = “geeksforgeeks”
Output: B
“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”
Output: A

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

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:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the winner of the game ` `void` `find_winner(string str, ``int` `n) ` `{ ` ` `  `    ``// To store the strings for both the players ` `    ``string str1 = ``""``, str2 = ``""``; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If the index is even ` `        ``if` `(i % 2 == 0) { ` ` `  `            ``// Append the current character ` `            ``// to player A's string ` `            ``str1 += str[i]; ` `        ``} ` ` `  `        ``// If the index is odd ` `        ``else` `{ ` ` `  `            ``// Append the current character ` `            ``// to player B's string ` `            ``str2 += str[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Sort both the strings to get ` `    ``// the lexicographically smallest ` `    ``// string possible ` `    ``sort(str1.begin(), str1.end()); ` `    ``sort(str2.begin(), str2.end()); ` ` `  `    ``// Copmpare both the strings to ` `    ``// find the winner of the game ` `    ``if` `(str1 < str2) ` `        ``cout << ``"A"``; ` `    ``else` `if` `(str2 < str1) ` `        ``cout << ``"B"``; ` `    ``else` `        ``cout << ``"Tie"``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeksforgeeks"``; ` `    ``int` `n = str.length(); ` ` `  `    ``find_winner(str, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.Arrays; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find the winner of the game ` `static` `void` `find_winner(String str, ``int` `n) ` `{ ` ` `  `    ``// To store the strings for both the players ` `    ``String str1 = ``""``, str2 = ``""``; ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// If the index is even ` `        ``if` `(i % ``2` `== ``0``)  ` `        ``{ ` ` `  `            ``// Append the current character ` `            ``// to player A's string ` `            ``str1 += str.charAt(i); ` `        ``} ` ` `  `        ``// If the index is odd ` `        ``else` `        ``{ ` ` `  `            ``// Append the current character ` `            ``// to player B's string ` `            ``str2 += str.charAt(i); ` `        ``} ` `    ``} ` ` `  `    ``// Sort both the strings to get ` `    ``// the lexicographically smallest ` `    ``// string possible ` `    ``char` `a[] = str1.toCharArray(); ` `    ``Arrays.sort(a); ` `    ``char` `b[] = str2.toCharArray(); ` `    ``Arrays.sort(b); ` `    ``str1 = ``new` `String(a); ` `    ``str2 = ``new` `String(b); ` ` `  `    ``// Copmpare both the strings to ` `    ``// find the winner of the game ` `    ``if` `(str1.compareTo(str2) < ``0``) ` `        ``System.out.print(``"A"``); ` `    ``else` `if` `(str1.compareTo(str2) > ``0``) ` `        ``System.out.print(``"B"``); ` `    ``else` `        ``System.out.print(``"Tie"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String str = ``"geeksforgeeks"``; ` `    ``int` `n = str.length(); ` ` `  `    ``find_winner(str, n); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to find the winner of the game  ` `def` `find_winner(string, n) : ` ` `  `    ``# To store the strings for both the players  ` `    ``string1``=` `"``"; string2 = "``";  ` `    ``for` `i ``in` `range``(n) : ` ` `  `        ``# If the index is even  ` `        ``if` `(i ``%` `2` `=``=` `0``) : ` ` `  `            ``# Append the current character  ` `            ``# to player A's string  ` `            ``string1 ``+``=` `string[i];  ` `         `  `        ``# If the index is odd  ` `        ``else` `:  ` `             `  `            ``# Append the current character  ` `            ``# to player B's string  ` `            ``string2 ``+``=` `string[i]; ` `             `  `    ``# Sort both the strings to get ` `    ``# the lexicographically smallest ` `    ``# string possible ` `    ``string1 ``=` `"".join(``sorted``(string1)) ` `    ``string2 ``=` `"".join(``sorted``(string2)) ` `     `  `    ``# Copmpare both the strings to  ` `    ``# find the winner of the game  ` `    ``if` `(string1 < string2) : ` `        ``print``(``"A"``, end ``=` `"");  ` `         `  `    ``elif` `(string2 < string1) : ` `        ``print``(``"B"``, end ``=` `""); ` `         `  `    ``else` `: ` `        ``print``(``"Tie"``, end ``=` `""); ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``string ``=` `"geeksforgeeks"``;  ` `    ``n ``=` `len``(string);  ` ` `  `    ``find_winner(string, n);  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find the winner of the game ` `static` `void` `find_winner(String str, ``int` `n) ` `{ ` ` `  `    ``// To store the strings for both the players ` `    ``String str1 = ``""``, str2 = ``""``; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` ` `  `        ``// If the index is even ` `        ``if` `(i % 2 == 0)  ` `        ``{ ` ` `  `            ``// Append the current character ` `            ``// to player A's string ` `            ``str1 += str[i]; ` `        ``} ` ` `  `        ``// If the index is odd ` `        ``else` `        ``{ ` ` `  `            ``// Append the current character ` `            ``// to player B's string ` `            ``str2 += str[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Sort both the strings to get ` `    ``// the lexicographically smallest ` `    ``// string possible ` `    ``char` `[]a = str1.ToCharArray(); ` `    ``Array.Sort(a); ` `    ``char` `[]b = str2.ToCharArray(); ` `    ``Array.Sort(b); ` `    ``str1 = ``new` `String(a); ` `    ``str2 = ``new` `String(b); ` ` `  `    ``// Copmpare both the strings to ` `    ``// find the winner of the game ` `    ``if` `(str1.CompareTo(str2) < 0) ` `        ``Console.Write(``"A"``); ` `    ``else` `if` `(str1.CompareTo(str2) > 0) ` `        ``Console.Write(``"B"``); ` `    ``else` `        ``Console.Write(``"Tie"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String str = ``"geeksforgeeks"``; ` `    ``int` `n = str.Length; ` ` `  `    ``find_winner(str, n); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```B
```

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.

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.

Improved By : AnkitRai01, Rajput-Ji

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.