Skip to content

Practice Remove Colored Pieces if Both Neighbors are the Same Color

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2038
LeetCode Problem

Remove Colored Pieces if Both Neighbors are the Same Color

Problem Statement

Alice and Bob alternate removals, with Alice first. Alice may remove an A only when both immediate neighbors are A; Bob has the same rule for B. Endpoint pieces cannot be removed. A player unable to move loses. Return whether Alice wins when both play optimally.

Source: Remove Colored Pieces if Both Neighbors are the Same Color on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
colors = "AAABABB"
Output
true

# Example 2

Input
colors = "AA"
Output
false

# Example 3

Input
colors = "ABBBBBBBAAA"
Output
false

Constraints

1 <= colors.length <= 10^5
colors consists of only the letters 'A' and 'B'

Related LeetCode Problems

Practice more problems similar to Remove Colored Pieces if Both Neighbors are the Same Color.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.