Decide whether Alice wins a game of removing pieces flanked by two same-colored neighbors

Quick Overview

Decide the winner of a two-player game on a string of A and B pieces, where each player may only remove a piece of their own color whose two neighbors share that color, and a player with no move loses. Tests game reasoning under optimal play and a linear scan over runs of equal characters.

Decide whether Alice wins a game of removing pieces flanked by two same-colored neighbors

Company: Unity

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 `i`th 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 ```python 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 ```text 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. ```text Input: colors = "AA" Output: False ``` No `'A'` has two `'A'` neighbors, so Alice cannot make her first move and loses. ```text 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.

Overview: Decide the winner of a two-player game on a string of A and B pieces, where each player may only remove a piece of their own color whose two neighbors share that color, and a player with no move loses. Tests game reasoning under optimal play and a linear scan over runs of equal characters.

Unity logo
Unity
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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.