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 stack 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 in nim-game
- Find the winner of the 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
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Find the player who will win the Coin game
- Game Theory (Normal - form game) | Set 1 (Introduction)
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.