Table of Contents
Description: Remove Colored Pieces if Both Neighbors are the Same Color
There are n
pieces arranged in a line, and each piece is colored either by 'A'
or by 'B'
. You are given a string colors
of length n
where colors[i]
is the color of the ith
piece.
Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.
- Alice is only allowed to remove a piece colored
'A'
if both its neighbors are also colored'A'
. She is not allowed to remove pieces that are colored'B'
. - Bob is only allowed to remove a piece colored
'B'
if both its neighbors are also colored'B'
. He is not allowed to remove pieces that are colored'A'
. - Alice and Bob cannot remove pieces from the edge of the line.
- If a player cannot make a move on their turn, that player loses and the other player wins.
Assuming Alice and Bob play optimally, return true
if Alice wins, or return false
if Bob wins.
Example 1
1 2 3 4 5 6 7 8 9 10 |
<strong>Input:</strong> colors = "AAABABB" <strong>Output:</strong> true <strong>Explanation:</strong> AAABABB -> AABABB Alice moves first. She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'. 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 2
1 2 3 4 5 6 |
<strong>Input:</strong> colors = "AA" <strong>Output:</strong> false <strong>Explanation:</strong> 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 3
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<strong>Input:</strong> colors = "ABBBBBBBAAA" <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBAAA -> ABBBBBBBAA Alice moves first. Her only option is to remove the second to last 'A' from the right. ABBBBBBBAA -> ABBBBBBAA Next is Bob's turn. He has many options for which 'B' piece to remove. He can pick any. On Alice's second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. |
Constraints
1 <= colors.length <= 105
colors
consists of only the letters'A'
and'B'
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class Solution { public boolean winnerOfGame(String colors) { int countA = 0; int a = 0; //count subsequences of As having number of As greater than 2 //AAAA -> number of chances Alice would get = 2 for(int i=0;i<colors.length();i++){ char c = colors.charAt(i); if(c=='A'){ a++; }else { if(a>2){ countA += (a-2); } a=0; } } if(a>2){ countA+= (a-2); } //count subsequences of Bs having number of Bs greater than 2 //BBBBB -> number of chances Alice would get = 3 int countB = 0; int b = 0; for(int i=0;i<colors.length();i++){ char c = colors.charAt(i); if(c=='B'){ b++; } else { if(b>2){ countB += (b-2); } b=0; } } if(b>2){ countB += (b-2); } return countA > countB; } } |
Time Complexity
O(n), where n is the number of characters in the string
Space Complexity
O(1)