#2038
Remove Colored Pieces if Both Neighbors are the Same Color
specialist · 670 · lc medium +30 · verified · 63.1% accepted · 1,636 likes · top 65%
Description
Given a string colors of 'A' and 'B' pieces, Alice and Bob alternate turns (Alice first). Alice may only remove a non-edge 'A' piece whose both neighbors are 'A'; Bob may only remove a non-edge 'B' piece whose both neighbors are 'B'. A player who cannot move loses. Both play optimally. Return true if Alice wins, false if Bob wins.
Example 1:
Input: colors = "AAABABB"
Output: true
Explanation:
AAABABB -> AABABB
Alice moves first.
She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
Example 2:
Now it's Bob's turn.
Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
Thus, Alice wins, so return true.
Example 3:
Input: colors = "AA"
Output: false
Explanation:
Alice has her turn first.
There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
Thus, Bob wins, so return false.
Example 4:
Input: colors = "ABBBBBBBAAA"
Output: false
Explanation:
ABBBBBBBAAA -> ABBBBBBBAA
Alice moves first.
Her only option is to remove the second to last 'A' from the right.
Example 5:
ABBBBBBBAA -> ABBBBBBAA
Next is Bob's turn.
He has many options for which 'B' piece to remove. He can pick any.
Example 6:
On Alice's second turn, she has no more pieces that she can remove.
Thus, Bob wins, so return false.
Code
1
2
3