Given a string consisting of lower case alphabets.
Rules of the Game:
- A player can choose a pair of similar consecutive characters and erase them.
- There are two players playing the game, the player who makes the last move wins.
The task is to find the winner if A goes first and both play optimally.
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
Approach: We can use a stack to simplify the problem.
- Each time we encounter a character that is different from the one present in the top of the stack we add it to the stack.
- If the stack top and the next character match we pop the character from the tack and increment the count.
- At the end, we just need to see who wins by checking count%2.
Below is the implementation of the above approach:
- Find the winner of the Game
- Find the winner in nim-game
- Find the winner of the game with N piles of boxes
- Predict the winner in Coin Game
- Predict the winner of the game | Sprague-Grundy
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- A modified game of Nim
- Variation in Nim Game
- Implementation of Tic-Tac-Toe game
- Optimal Strategy for a Game | DP-31
- Check if the game is valid or not
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.