![]() |
VOOZH | about |
Given a Binary String S. The task is to determine the winner of the game when two players play a game optimally with the string as per the given conditions:
Input: S = β1110011001010β
Output: Player 1
Explanation:
The selected characters will be in bold and Player 1's score is Score_1 and Player 2's score is Score_2 :
Turn 1 : (Player 1) β1110011001010β β β0011001010β Score_1 = 3
Turn 2 : (Player 2) β0011001010β β β00001010β Score_2 = 2
Turn 3 : (Player 1) β00001010ββ β0000010β Score_1 = 4
Turn 4: (Player 2) β0000010β
He cannot do anything as only one '1' is present which is an odd number.
Also, he can't choose the '0's as they are odd (5 and 1), Therefore, Score_2 =2
Turn 5:(Player 1) β0000010ββ β000000β Score_1=5
Turn 6:(Player 2) β000000β β ββ Score_2 = 2 (No '1' was deleted in this turn)Final scores: Score_1 = 5 and Score_2 = 2
Therefore, Player 1 wins.Input : S = β11111101β
Output: Player 2
Explanation:
Turn 1 : (Player 1) β11111101β β β1111110β Score_1 = 3
Turn 2 : (Player 2) β1111110β β β0β Score_2 = 6
Turn 3 : (Player 1) β0β β ββ Score_1 = 3Final scores: Score_1 = 3 and Score_2 = 6
Therefore, Player 2 wins.
Approach:
Below is the implementation of the above approach.
Output:
Player 2
Time Complexity: O(N)
Auxiliary Space: O(N)