[LeetCode] Remove Colored Pieces if Both Neighbors are the Same Color

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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool winnerOfGame(string c) {
int a = 0, b = 0;
for(int i = 1; i < c.length() - 1; i++) {
if(c[i] == c[i-1] and c[i] == c[i+1]) {
a += c[i] == 'A';
b += c[i] == 'B';
}
}
return a > b;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/11/PS/LeetCode/remove-colored-pieces-if-both-neighbors-are-the-same-color/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.