Remove Colored Pieces if Both Neighbors are the Same Color Solution
Remove Colored Pieces if Both Neighbors are the Same Color solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Remove Colored Pieces if Both Neighbors are the Same Color is a medium coding problem on strings, math and greedy, asked in Amazon, Google, Microsoft and Yelp interviews.
- Difficulty: Medium
- Topics: Strings, Math, Greedy, Game Theory
- Asked at: Amazon, Google, Microsoft, Yelp, MathWorks
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A row of pieces is described by the string colors, where each piece is either 'A' or 'B'. Alice and Bob take turns removing pieces, Alice first:
- Alice may remove a piece colored
'A'only if both of its neighbors are also'A'. - Bob may remove a piece colored
'B'only if both of its neighbors are also'B'. - Pieces at either end of the row have only one neighbor, so they can never be removed.
The player who cannot move on their turn loses. Both play optimally. Return true if Alice wins and false if Bob wins.
Example 1
Input: colors = "AAAABBB"
Output: true
Explanation: Alice has two possible removals inside `AAAA`, Bob only one inside `BBB`. Bob runs out first.
Example 2
Input: colors = "AABBBBA"
Output: false
Explanation: Alice has no legal move at all.
Example 3
Input: colors = "ABA"
Output: false
Constraints
1 <= colors.length <= 10^5colors consists of only the letters 'A' and 'B'
Hints for Remove Colored Pieces if Both Neighbors are the Same Color
Hint 1
Does one player's move ever create or destroy a move for the other player?
Hint 2
Removing an
Afrom the middle of anAblock only shortens that block; it never touches aBblock, and it never creates a new triple ofAs.Hint 3
So each player has a fixed budget of moves: a block of
kequal letters givesk - 2moves. Alice wins exactly when her budget is strictly larger.
How to solve Remove Colored Pieces if Both Neighbors are the Same Color
The players never interfere: every 'A' removal shortens an A block by one and leaves all B blocks untouched. So each player simply has a fixed number of moves, and Alice needs strictly more.
Approach
- Count
a, the positionsiwithcolors[i-1] = colors[i] = colors[i+1] = 'A', andb, the same for'B'. - Return
a > b.
Why it works
A block of k consecutive As allows exactly k - 2 removals (each one shrinks the block by one until two remain), whatever order they come in, and nothing Bob does changes that block because he only removes interior Bs. The same holds for Bob. The game is therefore a race: Alice moves on turns 1, 3, 5, … and loses as soon as she needs move a + 1, which happens before Bob needs move b + 1 exactly when a <= b.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Ties go to Bob: with equal budgets Alice runs out first because she moves first.
- End pieces never count — the scan runs from index 1 to
n - 2. - There is no need to simulate the game; the budgets are fixed from the start.
Reference solution
Python
def winnerOfGame(colors: str) -> bool:
a = b = 0
for i in range(1, len(colors) - 1):
if colors[i - 1] == colors[i] == colors[i + 1]:
if colors[i] == "A":
a += 1
else:
b += 1
return a > bJava
public static boolean winnerOfGame(String colors) {
int a = 0, b = 0;
for (int i = 1; i + 1 < colors.length(); i++) {
char c = colors.charAt(i);
if (colors.charAt(i - 1) == c && colors.charAt(i + 1) == c) {
if (c == 'A') a++;
else b++;
}
}
return a > b;
}C++
bool winnerOfGame(string colors) {
int a = 0, b = 0;
for (int i = 1; i + 1 < (int)colors.size(); i++) {
if (colors[i - 1] == colors[i] && colors[i] == colors[i + 1]) {
if (colors[i] == 'A') a++;
else b++;
}
}
return a > b;
}JavaScript
var winnerOfGame = function(colors) {
var a = 0, b = 0;
for (var i = 1; i + 1 < colors.length; i++) {
if (colors[i - 1] === colors[i] && colors[i] === colors[i + 1]) {
if (colors[i] === "A") a++;
else b++;
}
}
return a > b;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Greedy Algorithms