2038. Remove Colored Pieces if Both Neighbors Are the Same Color

2038. Remove Colored Pieces if Both Neighbors are the Same Color #

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.

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.

Constraints:

  • 1 <= colors.length <= 100000
  • colors consists of only the letters ‘A’ and ‘B’

Problem Summary #

There are a total of n colored pieces arranged in a line, and each colored piece is either ‘A’ or ‘B’. You are given a string colors of length n, where colors[i] represents the color of the ith colored piece.

Alice and Bob are playing a game where they take turns deleting colors from this string. Alice goes first.

  • If a colored piece is ‘A’ and its two adjacent colors are both ‘A’, then Alice can delete that colored piece. Alice cannot delete any colored piece of color ‘B’.
  • If a colored piece is ‘B’ and its two adjacent colors are both ‘B’, then Bob can delete that colored piece. Bob cannot delete any colored piece of color ‘A’.
  • Alice and Bob cannot delete colored pieces from the two ends of the string.
  • If one of them cannot continue to make a move, that player loses the game and the other player wins.

Assuming both Alice and Bob use optimal strategies, return true if Alice wins; otherwise, Bob wins, return false.

Solution Approach #

  • Count the number of moves Alice and Bob can make, denoted as As and Bs respectively
  • Since Alice goes first, as long as As is greater than Bs, Alice wins and true is returned; otherwise, Bob wins and false is returned

Code #

package leetcode

func winnerOfGame(colors string) bool {
	As, Bs := 0, 0
	Acont, Bcont := 0, 0
	for _, color := range colors {
		if color == 'A' {
			Acont += 1
			Bcont = 0
		} else {
			Bcont += 1
			Acont = 0
		}
		if Acont >= 3 {
			As++
		}
		if Bcont >= 3 {
			Bs++
		}
	}
	if As > Bs {
		return true
	}
	return false
}

Calendar Jun 28, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文