Dos jugadores están jugando un juego en el que se da una string str . El primer jugador puede tomar los caracteres en índices pares y el segundo jugador puede tomar los caracteres en índices impares. El jugador que puede construir la string lexicográficamente más pequeña que el otro jugador gana el juego. Imprime el ganador del juego, ya sea el jugador A , B o imprime Empate si es un empate.
Ejemplos:
Entrada: str = “geeksforgeeks”
Salida: B
“eeggoss” es la
string lexicográficamente más pequeña que puede obtener el jugador A.
“eefkkr” es la
string lexicográficamente más pequeña que puede obtener el jugador B.
Y la string de B es lexicográficamente más pequeña.
Entrada: str = “abcdbh”
Salida: A
Enfoque: Cree dos strings vacías str1 y str2 para el jugador A y B respectivamente. Recorra la string original carácter por carácter y para cada carácter cuyo índice sea par, agregue este carácter en str1 ; de lo contrario, agregue este carácter en str2 . Finalmente ordene la string generada para obtener la string lexicográficamente más pequeña posible y compárela para encontrar el ganador del juego.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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()); // Compare 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); // Compare 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)) # Compare 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); // Compare 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
Javascript
<script> // Javascript implementation of the approach // Function to find the winner of the game function find_winner(str, n) { // To store the strings for both the players var str1 = "", str2 = ""; for (var 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 str1 = str1.split('').sort(); str2 = str2.split('').sort(); // Compare both the strings to // find the winner of the game if (str1 < str2) document.write( "A"); else if (str2 < str1) document.write( "B"); else document.write( "Tie"); } // Driver code var str = "geeksforgeeks"; var n = str.length; find_winner(str, n); </script>
B