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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find the player who will win the Coin game
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the winner in nim-game
- Find the winner of the game with N piles of boxes
- Find the winner of the Game
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Determine winner of the Game by arranging balls in a row
- Determine the winner of a game of deleting Characters from a String
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Check if any K ranges overlap at any point
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.