Two players are playing a game where 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 it’s a tie.
Examples:
Input: str = “geeksforgeeks”
Output: B
Explanation: “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
Approach: Create two empty strings str1 and str2 for players 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 approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_winner(string str, int n)
{
string str1 = "" , str2 = "" ;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
str1 += str[i];
}
else {
str2 += str[i];
}
}
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
if (str1 < str2)
cout << "A" ;
else if (str2 < str1)
cout << "B" ;
else
cout << "Tie" ;
}
int main()
{
string str = "geeksforgeeks" ;
int n = str.length();
find_winner(str, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static void find_winner(String str, int n)
{
String str1 = "" , str2 = "" ;
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 ) {
str1 += str.charAt(i);
}
else {
str2 += str.charAt(i);
}
}
char a[] = str1.toCharArray();
Arrays.sort(a);
char b[] = str2.toCharArray();
Arrays.sort(b);
str1 = new String(a);
str2 = new String(b);
if (str1.compareTo(str2) < 0 )
System.out.print( "A" );
else if (str1.compareTo(str2) > 0 )
System.out.print( "B" );
else
System.out.print( "Tie" );
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.length();
find_winner(str, n);
}
}
|
Python3
def find_winner(string, n):
string1 = ""
string2 = ""
for i in range (n):
if (i % 2 = = 0 ):
string1 + = string[i]
else :
string2 + = string[i]
string1 = "".join( sorted (string1))
string2 = "".join( sorted (string2))
if (string1 < string2):
print ( "A" , end = "")
elif (string2 < string1):
print ( "B" , end = "")
else :
print ( "Tie" , end = "")
if __name__ = = "__main__" :
string = "geeksforgeeks"
n = len (string)
find_winner(string, n)
|
C#
using System;
class GFG {
static void find_winner(String str, int n)
{
String str1 = "" , str2 = "" ;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
str1 += str[i];
}
else {
str2 += str[i];
}
}
char [] a = str1.ToCharArray();
Array.Sort(a);
char [] b = str2.ToCharArray();
Array.Sort(b);
str1 = new String(a);
str2 = new String(b);
if (str1.CompareTo(str2) < 0)
Console.Write( "A" );
else if (str1.CompareTo(str2) > 0)
Console.Write( "B" );
else
Console.Write( "Tie" );
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.Length;
find_winner(str, n);
}
}
|
Javascript
<script>
function find_winner(str, n)
{
var str1 = "" , str2 = "" ;
for ( var i = 0; i < n; i++) {
if (i % 2 == 0) {
str1 += str[i];
}
else {
str2 += str[i];
}
}
str1 = str1.split( '' ).sort();
str2 = str2.split( '' ).sort();
if (str1 < str2)
document.write( "A" );
else if (str2 < str1)
document.write( "B" );
else
document.write( "Tie" );
}
var str = "geeksforgeeks" ;
var n = str.length;
find_winner(str, n);
</script>
|
Time Complexity: O(n log(n)), Where n is the length of the given string.
Auxiliary Space: O(n), for storing the strings for both the players.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Aug, 2022
Like Article
Save Article