Remove Colored Pieces if Both Neighbors are the Same Color
Problem Description​
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.
Examples​
Example 1:
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 = "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 <= 10^5
colors consists of only the letters 'A' and 'B'
Solution for Remove Colored Pieces if Both Neighbors are the Same Color​
Approach​
-
Initialization: Start with two counters,
countA
andcountB
, initialized to zero. These will keep track of the number of triplets 'AAA' and 'BBB' found in the stringcolors
. -
Iteration: Loop through the string
colors
from the beginning to the third last character (n - 2
), wheren
is the length ofcolors
. -
Counting Triplets:
- Check for sequences of three consecutive 'A's (
'AAA'
). If found, incrementcountA
. - Check for sequences of three consecutive 'B's (
'BBB'
). If found, incrementcountB
.
- Check for sequences of three consecutive 'A's (
-
Comparison: After iterating through the string:
- Compare
countA
andcountB
. - Return
true
ifcountA
(number of 'AAA' sequences) is greater thancountB
(number of 'BBB' sequences), indicating player 'A' has more winning sequences. - Otherwise, return
false
.
- Compare
- Solution
Implementation​
function Solution(arr) { function winnerOfGame(colors) { let countA = 0, countB = 0; const n = colors.length; for (let i = 0; i < n - 2; i++) { if (colors[i] === 'A' && colors[i + 1] === 'A' && colors[i + 2] === 'A') { countA++; } if (colors[i] === 'B' && colors[i + 1] === 'B' && colors[i + 2] === 'B') { countB++; } } return countA > countB; } const input = "AAABABB" const output =winnerOfGame(input) return ( <div> <p> <b>Input: </b> {JSON.stringify(input)} </p> <p> <b>Output:</b> {output.toString()} </p> </div> ); }
Complexity Analysis​
- Time Complexity:
- Space Complexity:
Code in Different Languages​
- JavaScript
- TypeScript
- Python
- Java
- C++
function winnerOfGame(colors) {
let countA = 0, countB = 0;
const n = colors.length;
for (let i = 0; i < n - 2; i++) {
if (colors[i] === 'A' && colors[i + 1] === 'A' && colors[i + 2] === 'A') {
countA++;
}
if (colors[i] === 'B' && colors[i + 1] === 'B' && colors[i + 2] === 'B') {
countB++;
}
}
return countA > countB;
}
function winnerOfGame(colors: string): boolean {
let countA: number = 0, countB: number = 0;
const n: number = colors.length;
for (let i: number = 0; i < n - 2; i++) {
if (colors[i] === 'A' && colors[i + 1] === 'A' && colors[i + 2] === 'A') {
countA++;
}
if (colors[i] === 'B' && colors[i + 1] === 'B' && colors[i + 2] === 'B') {
countB++;
}
}
return countA > countB;
}
def winner_of_game(colors: str) -> bool:
countA = countB = 0
n = len(colors)
for i in range(n - 2):
if colors[i] == 'A' and colors[i + 1] == 'A' and colors[i + 2] == 'A':
countA += 1
if colors[i] == 'B' and colors[i + 1] == 'B' and colors[i + 2] == 'B':
countB += 1
return countA > countB
public class Solution {
public boolean winnerOfGame(String colors) {
int countA = 0, countB = 0;
int n = colors.length();
for (int i = 0; i < n - 2; i++) {
if (colors.charAt(i) == 'A' && colors.charAt(i + 1) == 'A' && colors.charAt(i + 2) == 'A') {
countA++;
}
if (colors.charAt(i) == 'B' && colors.charAt(i + 1) == 'B' && colors.charAt(i + 2) == 'B') {
countB++;
}
}
return countA > countB;
}
}
class Solution {
public:
bool winnerOfGame(string colors) {
int countA = 0, countB = 0;
int n = colors.length();
for (int i = 0; i < n - 2; i++) {
if (colors[i] == 'A' && colors[i + 1] == 'A' && colors[i + 2] == 'A') {
countA++;
}
if (colors[i] == 'B' && colors[i + 1] == 'B' && colors[i + 2] == 'B') {
countB++;
}
}
return countA > countB;
}
};
References​
-
LeetCode Problem: Remove Colored Pieces if Both Neighbors are the Same Color
-
Solution Link: LeetCode Solution