There are n pieces arranged in a line, and each piece is colored either 'A' or 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.
Alice and Bob play a game where they take alternating turns removing pieces from the line. Alice moves first.
-
Alice may only remove a piece colored
'A'
if both its neighbors are also colored
'A'
.
-
Bob may only remove a piece colored
'B'
if both its neighbors are also colored
'B'
.
-
Neither player may remove a piece from either edge of the line.
-
A player who cannot make a move on their turn loses, and the other player wins.
Assuming Alice and Bob both play optimally, return whether Alice wins.
Function Signature
def alice_wins(colors: str) -> bool:
Rules
-
Return
True
if Alice wins and
False
otherwise.
-
After a removal the line closes up, so the pieces on either side of the removed piece become neighbors.
Constraints
-
1 <= len(colors) <= 10^5
-
colors
consists only of the letters
'A'
and
'B'
.
Examples
Input: colors = "AAABABB"
Output: True
Alice removes the middle 'A' of "AAA", leaving "AABABB". No 'B' has 'B' on both sides, so Bob cannot move on his first turn and loses.
Input: colors = "AA"
Output: False
No 'A' has two 'A' neighbors, so Alice cannot make her first move and loses.
Input: colors = "ABBBBBBBAAA"
Output: False
Alice has one legal removal (the middle of "AAA") and Bob has five (the interior of the run of seven 'B's). Alice uses her only move, Bob answers, and Alice has nothing left on her second turn.