Problem

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.

Examples

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'.

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:

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 3:

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.

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.

Solution

Method 1 - Count removable pieces

To determine the winner, assuming both players play optimally:

  1. Count Removable Pieces:
    • Alice can only remove pieces A that have A neighbours on both sides (i.e., "AAA").
    • Bob can only remove pieces B that have B neighbours on both sides (i.e., "BBB").
  2. Winning Condition:
    • We count the number of such removable As (let’s call this countA) and Bs (countB).
    • Alice goes first and can only remove As, and Bob goes second and can only remove Bs.
    • If countA > countB, Alice will have more opportunities to remove pieces than Bob, leading to Alice winning.
    • If countA <= countB, Bob will either have equal or more opportunities, ensuring Bob wins or at least denies Alice her first move when the game ends.

Code

Java
public class Solution {
    public boolean winnerOfGame(String colors) {
        int countA = 0, countB = 0;
        int n = colors.length();
        for (int i = 1; i < n - 1; i++) {
            if (colors.charAt(i - 1) == 'A' && colors.charAt(i) == 'A' && colors.charAt(i + 1) == 'A') {
                countA++;
            }
            if (colors.charAt(i - 1) == 'B' && colors.charAt(i) == 'B' && colors.charAt(i + 1) == 'B') {
                countB++;
            }
        }
        return countA > countB;
    }
}
Python
class Solution:
    def winnerOfGame(self, colors: str) -> bool:
        countA: int = 0
        countB: int = 0
        n: int = len(colors)
        
        for i in range(1, n - 1):
            if colors[i - 1] == 'A' and colors[i] == 'A' and colors[i + 1] == 'A':
                countA += 1
            if colors[i - 1] == 'B' and colors[i] == 'B' and colors[i + 1] == 'B':
                countB += 1
        
        return countA > countB

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the string colors, as we only need a single pass to count countA and countB.
  • 🧺 Space complexity: O(1) since no extra space proportional to input size is needed, just a few counters.