Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Flip Game II
EASY
15 mins
7 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Flip Game II

Contributed by
Prateek_18cb
Easy
yellow-spark
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share
7 upvotes

Problem Statement

Ninja and his friend are playing a game called flip game. They are given a string ‘STR’ containing only two characters, ‘$’ and ‘*.’

In this game, Ninja and his friend take turns to flip two consecutive “$$” to “**”. The flip game ends when Ninja or his friend can no longer make a move, i.e., there is no consecutive “$$” present in the ‘STR’ and, therefore, the other person will be the winner of the game.

Both the players play the game optimally in alternate turns. Given that Ninja starts the game i.e. takes the first turn, your task is to find out if he wins the game.

For example:
Let 'STR' = "$$**".

There are consecutive "$$" in 'STR' and it is Ninja's turn to begin the game. So, Ninja changes the consecutive "$$" to "**". This changes 'STR' to "****". 

Now, as it is his friend's turn and there are no more consecutive "$$", Ninja wins the game.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
$$$$
****
Sample Output 1:
true
false
Explanation for Sample Output 1:
For sample test case 1: 

In the first move, Ninja changes the string “$$$$” to “$**$”.
Now there is no consecutive “$$” present so Ninja’s friend can not make any move. Hence Ninja wins the game.

For sample test case 2:

There is no consecutive “$$” present so Ninja can not make a move. Hence his friend wins the game.
Sample Input 2:
2
$$$$$
**$$**$
Sample Output 2:
false
true
Explanation for Sample Output 2:
For sample test case 1: 

If the first move, Ninja changes the string “$$$$$” to “**$$$”.
Then his friend changes the string “**$$$” to “****$”.
Now there is no consecutive “$$” present so Ninja can not make any move. Hence his friend wins the game.

For sample test case 2: 

If the first move, Ninja changes the string “**$$**$” to “******$”.
Now there is no consecutive “$$” present so Ninja’s friend can not make any move. Hence Ninja wins the game.
Reset Code
Full screen
Auto
copy-code
Console