Imagine you're playing a strategic puzzle game similar to the classic Zuma! You have a row of colored balls on a board, where each ball can be 'R' (red), 'Y' (yellow), 'B' (blue), 'G' (green), or 'W' (white). In your hand, you have several additional colored balls that you can strategically place.

Your mission: Clear all balls from the board using the minimum number of balls from your hand!

Game Rules:

  • On each turn, pick any ball from your hand and insert it anywhere in the row (between two balls or at either end)
  • After insertion, if there are 3 or more consecutive balls of the same color, they disappear automatically
  • This removal might create new groups of 3+ consecutive balls - these also disappear in a chain reaction
  • Continue until no more groups can be removed
  • Win condition: All balls cleared from the board

Challenge: Given the initial board configuration and the balls in your hand, find the minimum number of balls you need to insert to clear the entire board. Return -1 if it's impossible.

Input & Output

example_1.py โ€” Basic Case
$ Input: board = "WWRRBBWW", hand = "WRBRW"
โ€บ Output: 2
๐Ÿ’ก Note: Place 'R' at position 2: "WWRRRBBWW" โ†’ Remove RRR: "WWBBWW". Then place 'B' at position 3: "WWBBBWW" โ†’ Remove BBB: "WWWW" โ†’ Remove WWWW: "". Total: 2 moves.
example_2.py โ€” Impossible Case
$ Input: board = "RWWWRR", hand = "RRB"
โ€บ Output: -1
๐Ÿ’ก Note: No matter how we place the balls from our hand, we cannot clear the entire board. The WWW in the middle cannot be removed with the available balls.
example_3.py โ€” Chain Reaction
$ Input: board = "G", hand = "GGGGG"
โ€บ Output: 2
๐Ÿ’ก Note: Place 'G' at position 0: "GG", then place another 'G': "GGG" โ†’ Remove GGG: "". Total: 2 moves needed to form a group of 3.

Visualization

Tap to expand
Zuma Game: Strategic Ball PlacementInitial Board:RWWWRRStrategic Positions (Green Arrows):StartBefore RREndNon-Strategic Positions (Red X):โœ—Middle of WWWโœ—Between W-WBFS Level Exploration:Level 0: Initial state | Level 1: Try each strategic position | Level 2: Recurse...First empty board found = Minimum moves guaranteed!๐Ÿ’ก Key Insights:โ€ข Only try positions that can potentially create 3+ groupsโ€ข Use memoization to cache (board + sorted_hand) statesโ€ข BFS ensures minimum moves by exploring level-by-level
Understanding the Visualization
1
Analyze the Board
Identify existing groups and potential trigger points where adding 1-2 balls could create groups of 3+
2
Strategic Placement
Only consider positions adjacent to existing groups of the same color - these are the 'strategic positions'
3
Chain Reaction Simulation
After each placement, simulate the complete chain reaction of removing 3+ consecutive balls
4
BFS Exploration
Use BFS to explore all possibilities level by level, ensuring we find the minimum number of moves
5
Memoization Cache
Remember already-computed board states to avoid redundant work
Key Takeaway
๐ŸŽฏ Key Insight: The optimal solution combines BFS for guaranteed minimum moves with strategic positioning to dramatically reduce the search space, making an otherwise exponential problem tractable through smart pruning and memoization.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(4^n * m)

Where n is the maximum depth (moves), m is board length for processing. Pruned by memoization.

n
2n
โœ“ Linear Growth
Space Complexity
O(4^n * m)

BFS queue and memoization storage for unique states

n
2n
โšก Linearithmic Space

Constraints

  • board and hand have lengths in range [1, 16]
  • Each ball color is one of 'R', 'Y', 'B', 'G', or 'W'
  • The input strings contain only the specified ball colors
  • Game rule: Groups of 3 or more consecutive same-colored balls are removed
  • Objective: Clear all balls using minimum insertions from hand
Asked in
Google 15 Amazon 8 Meta 12 Microsoft 6
28.4K Views
Medium Frequency
~35 min Avg. Time
876 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen