You are playing a variation of the game Zuma. In this variation of Zuma, there is a single row of colored balls on a board, where each ball can be colored red 'R', yellow 'Y', blue 'B', green 'G', or white 'W'. You also have several colored balls in your hand.

Your goal is to clear all of the balls from the board. On each turn:

Pick any ball from your hand and insert it in between two balls in the row or on either end of the row.

If there is a group of three or more consecutive balls of the same color, remove the group of balls from the board.

If this removal causes more groups of three or more of the same color to form, then continue removing each group until there are none left.

If there are no more balls on the board, then you win the game.

Repeat this process until you either win or do not have any more balls in your hand.

Given a string board, representing the row of balls on the board, and a string hand, representing the balls in your hand, return the minimum number of balls you have to insert to clear all the balls from the board. If you cannot clear all the balls from the board using the balls in your hand, return -1.

Input & Output

Example 1 — Basic Clear
$ Input: board = "RWWWRR", hand = "RB"
Output: 2
💡 Note: Insert B at position 1: RBWWWRR → RBWWWRR (no removal). Insert B at position 2: RBBWWWRR → R___WWWRR (BBB removed) → RWWWRR. Insert R at position 3: RWWRRRR → RWW___ (RRR removed) → RWW. Continue to clear remaining.
Example 2 — Impossible Case
$ Input: board = "RWWWRR", hand = "WWBBRR"
Output: -1
💡 Note: No matter how we insert the balls, we cannot clear all balls from the board with the given hand.
Example 3 — Already Clear
$ Input: board = "", hand = "RWB"
Output: 0
💡 Note: Board is already empty, so no moves needed.

Constraints

  • 1 ≤ board.length ≤ 16
  • 0 ≤ hand.length ≤ 5
  • board and hand consist of characters 'R', 'Y', 'B', 'G', and 'W'
  • The initial row of balls on the board will not have any groups of three or more consecutive balls of the same color

Visualization

Tap to expand
Zuma Game - BFS Approach INPUT Board: R W W W R R Hand: R B board = "RWWWRR" hand = "RB" Goal: Clear all balls ALGORITHM STEPS 1 Init BFS Queue State: (board, hand, moves) 2 Try All Insertions Insert each hand ball at every position 3 Remove Matches Clear 3+ consecutive same-color balls 4 Check Result Empty board? Return moves Else continue BFS BFS Tree RWWWRR RRWWWRR RWWWRRR FINAL RESULT Solution Path: Move 1: Insert R RWWWRR + R at end = RWWWRRR --> RWWW (RRR removed) Move 2: Insert R RWWW + R at start = RRWWW --> "" (all cleared!) Output: 2 OK - Board Cleared! Minimum balls used: 2 Key Insight: BFS explores states level by level, guaranteeing the minimum number of insertions. Each state = (current board, remaining hand). We prune by using visited set to avoid duplicate states. Chain reactions are handled by repeatedly removing 3+ consecutive balls. TutorialsPoint - Zuma Game | BFS Approach
Asked in
Google 12 Microsoft 8 Apple 6
28.4K Views
Medium Frequency
~35 min Avg. Time
856 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