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.
'A'
if both its neighbors are also colored 'A'
. She is not allowed to remove pieces that are colored 'B'
.'B'
if both its neighbors are also colored 'B'
. He is not allowed to remove pieces that are colored 'A'
.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 <= 105
colors
consists of only the letters 'A'
and 'B'
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're playing a game where players remove colored pieces. The brute force way means we try absolutely every possible move sequence and see who wins each time. We then determine who wins overall by analyzing all these game outcomes.
Here's how the algorithm would work step-by-step:
def remove_colored_pieces_brute_force(colors):
def find_possible_moves(current_colors, player):
possible_moves = []
for index in range(1, len(current_colors) - 1):
if current_colors[index - 1] == player and current_colors[index + 1] == player and current_colors[index] == player:
possible_moves.append(index)
return possible_moves
def game_winner(current_colors, alice_turn):
alice_wins = 0
bob_wins = 0
possible_moves = find_possible_moves(current_colors, 'A' if alice_turn else 'B')
# If there are no moves, the other player wins
if not possible_moves:
return 'B' if alice_turn else 'A'
for move in possible_moves:
new_colors = current_colors[:move] + current_colors[move+1:]
winner = game_winner(new_colors, not alice_turn)
if winner == 'A':
alice_wins += 1
else:
bob_wins += 1
if alice_wins >= bob_wins:
return 'A'
else:
return 'B'
# Count wins in all scenarios, declare winner
alice_wins_total = 0
bob_wins_total = 0
winner = game_winner(colors, True)
if winner == 'A':
alice_wins_total += 1
else:
bob_wins_total += 1
# Determine overall winner
if alice_wins_total > bob_wins_total:
return True
else:
return False
The key to this problem is to focus on the potential moves each player can make instead of simulating the entire game. A player can only make a move if they are surrounded by their own color. By counting the number of possible moves for each player, we can quickly determine the winner.
Here's how the algorithm would work step-by-step:
def winnerOfGame(colors):
alice_moves = 0
bob_moves = 0
# Iterate through the colors to count moves.
for i in range(1, len(colors) - 1):
if colors[i - 1] == colors[i] == colors[i + 1]:
# Increment Alice's moves if the color is 'A'.
if colors[i] == 'A':
alice_moves += 1
# Increment Bob's moves if the color is 'B'.
else:
bob_moves += 1
# Determine the winner based on the number of moves.
if alice_moves > bob_moves:
return True
else:
return False
Case | How to Handle |
---|---|
Null or empty input string | Return false immediately since neither player can make any moves. |
String with length less than 3 | Return false immediately since no piece can have two neighbors. |
String with all 'A's or all 'B's | No moves are possible, so return false. |
String with alternating 'A's and 'B's (e.g., 'ABABAB') | No moves are possible, so return false. |
String with a long sequence of the same color (e.g., 'AAAAAAA') | The solution should efficiently count the number of removable pieces in the sequence without exceeding time limits. |
String with only one possible move for Alice or Bob | The algorithm should correctly identify and count only that single possible move. |
Very long input string (close to the maximum allowed length) | Ensure that the solution uses O(n) time and space complexity to avoid timeouts. |
String starts or ends with a sequence of repeating characters | The algorithm needs to correctly handle boundary conditions when calculating possible moves. |