Dada una string que consta de alfabetos en minúsculas.
Reglas del juego:
- Un jugador puede elegir un par de caracteres consecutivos similares y borrarlos.
- Hay dos jugadores jugando el juego, el jugador que hace el último movimiento gana.
La tarea es encontrar al ganador si A va primero y ambos juegan de manera óptima.
Ejemplos:
Input: str = "kaak" Output: B Explanation: Initial String: "kaak" A's turn: removes: "aa" Remaining String: "kk" B's turn: removes: "kk" Remaining String: "" Since B was the last one to play B is the winner. Input: str = "kk" Output: A
Enfoque: Podemos usar una pila para simplificar el problema.
- Cada vez que nos encontramos con un personaje que es diferente del presente en la parte superior de la pila, lo agregamos a la pila.
- Si la parte superior de la pila y el siguiente carácter coinciden, extraemos el carácter de la pila e incrementamos la cuenta.
- Al final, solo tenemos que ver quién gana comprobando count%2.
A continuación se muestra la implementación del enfoque anterior:
C++
#include <bits/stdc++.h> using namespace std; // Function to play the game // and find the winner void findWinner(string s) { int i, count = 0, n; n = s.length(); stack<char> st; // ckecking the top of the stack with // the i th character of the string // add it to the stack if they are different // otherwise increment count for (i = 0; i < n; i++) { if (st.empty() || st.top() != s[i]) { st.push(s[i]); } else { count++; st.pop(); } } // Check who has won if (count % 2 == 0) { cout << "B" << endl; } else { cout << "A" << endl; } } // Driver code int main() { string s = "kaak"; findWinner(s); return 0; }
Java
// Java implementation for above approach import java.util.*; class GFG { // Function to play the game // and find the winner static void findWinner(String s) { int i, count = 0, n; n = s.length(); Stack<Character> st = new Stack<Character>(); // ckecking the top of the stack with // the i th character of the string // add it to the stack if they are different // otherwise increment count for (i = 0; i < n; i++) { if (st.isEmpty() || st.peek() != s.charAt(i)) { st.push(s.charAt(i)); } else { count++; st.pop(); } } // Check who has won if (count % 2 == 0) { System.out.println("B"); } else { System.out.println("A"); } } // Driver code public static void main(String[] args) { String s = "kaak"; findWinner(s); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach # Function to play the game # and find the winner def findWinner(s) : count = 0 n = len(s); st = []; # ckecking the top of the stack with # the i th character of the string # add it to the stack if they are different # otherwise increment count for i in range(n) : if (len(st) == 0 or st[-1] != s[i]) : st.append(s[i]); else : count += 1; st.pop(); # Check who has won if (count % 2 == 0) : print("B"); else : print("A"); # Driver code if __name__ == "__main__" : s = "kaak"; findWinner(s); # This code is contributed by AnkitRai01
C#
// C# implementation for above approach using System; using System.Collections.Generic; class GFG { // Function to play the game // and find the winner static void findWinner(String s) { int i, count = 0, n; n = s.Length; Stack<char> st = new Stack<char>(); // ckecking the top of the stack with // the i th character of the string // add it to the stack if they are different // otherwise increment count for (i = 0; i < n; i++) { if (st.Count == 0 || st.Peek() != s[i]) { st.Push(s[i]); } else { count++; st.Pop(); } } // Check who has won if (count % 2 == 0) { Console.WriteLine("B"); } else { Console.WriteLine("A"); } } // Driver code public static void Main(String[] args) { String s = "kaak"; findWinner(s); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation for above approach // Function to play the game // and find the winner function findWinner(s) { let i, count = 0, n; n = s.length; let st = []; // ckecking the top of the stack with // the i th character of the string // add it to the stack if they are different // otherwise increment count for (i = 0; i < n; i++) { if (st.length == 0 || st[st.length - 1] != s[i]) { st.push(s[i]); } else { count++; st.pop(); } } // Check who has won if (count % 2 == 0) { document.write("B"); } else { document.write("A"); } } let s = "kaak"; findWinner(s); // This code is contributed by divyesh072019. </script>
Producción:
B
Publicación traducida automáticamente
Artículo escrito por Rafiu Jaman Mollah y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA