![]() |
VOOZH | about |
Given a row of silver coins among which a special gold coin is present. Two players play the game, and with each move, a player has to remove a coin from either the left or the right end of the row and the player who removes the special coin wins the game. The task is to find the winner of the game.
Examples:
Input: str = "GSSS"
Output: First
The first player directly removes the special gold coin.Input: str = "SGS"
Output: Second
Irrespective of which coin the first player removes, the special
gold coin becomes exposed and is removed by the second player.
Approach: It can be observed by taking a few examples that if the count of the silver coins is odd then the first player wins the game otherwise the player two wins the game. In special case when Gold coin is in corner, First player will be the winner regardless the count of silver coins.
Below are the steps to implement the above approach:
Below is the implementation of the above approach:
First
Time Complexity: O(n) where n is the size of the string.
Auxiliary Space: O(1)