Remove Colored Pieces if Both Neighbors are the Same Color

Medium Topics Company Tags

Alice and Bob alternate turns removing pieces from the string colors, with Alice moving first. Alice may remove an A only when its immediate neighbors are both A. Bob may remove a B only when its immediate neighbors are both B. End pieces cannot be removed. The remaining pieces close the gap after a removal.

A player who has no legal move loses. Assuming optimal play, return true if Alice wins and false if Bob wins.

Example 1:

Input: colors = "AAA"

Output: true

Explanation: Alice removes the middle A, leaving AA. Bob has no legal move, so Alice wins.

Example 2:

Input: colors = "AAABBB"

Output: false

Explanation: Alice must remove the middle A, leaving AABBB. Bob removes the middle B, leaving AABB. Alice then has no move and loses.

Example 3:

Input: colors = "AAAABBB"

Output: true

Explanation: Alice can remove two pieces from the initial run of four As, while Bob can remove only one B. After Alice, Bob, and Alice move, Bob has no legal move.

Constraints:

  • 1 <= colors.length <= 100000
  • Only A and B occur.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.