Skip to content
Problem 488 · LeetCode 488(opens in a new tab)

Zuma Game

HardBacktracking

A row of coloured balls sits on a board, written as a string board where each letter is a colour: R, Y, B, G or W. You also hold some balls, written as the string hand.

On each turn you take one ball from your hand and place it anywhere in the row: between two balls or at either end. Then any run of three or more touching balls of one colour disappears, and if that makes a new run of three or more, it disappears too, and so on.

Return the fewest balls you must place to empty the board, or −1 if your hand cannot do it.

Example 1

Input:  board = "WBBW", hand = "WB"
Output: 2
Why:    B makes WBBBW → WW; then W makes WWW → empty
2 more examples and the constraints

Example 2

Input:  board = "RYYR", hand = "Y"
Output: -1
Why:    RYYYR clears to RR, and no R is left to finish it

Example 3

Input:  board = "B", hand = "BBB"
Output: 2
Why:    two more Bs make BBB

Constraints

  • 1 ≤ length of board ≤ 16
  • 1 ≤ length of hand ≤ 5
  • Both strings use only R, Y, B, G and W
  • The starting board has no run of three or more of one colour

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Try every insert, with a memoexponential
  2. BFS with useful inserts onlyexponential, pruned

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 16-ball board and 5 different ballsVerdict
Try every insert, with a memoexponential
Correct, explores useless inserts
BFS with useful inserts onlyexponential, pruned
Best; few gaps per ball

n = balls on the board, m = balls in hand. Measured on hard boards, the pruned search reaches 5 to 40 times fewer states.

Edge cases to test

  • Impossible. RYYR with hand Y gives −1: return −1 once the queue is empty.
  • Chain reaction. One ball can clear several runs in a row; keep clearing until no run of three is left.
  • Split first. RRWWRRBBRR with hand WB needs a W between two Rs; the answer is 2.
  • More balls than needed. B with hand BBB uses only two; return the count used, not the hand size.
  • Colour not on the board. A ball whose colour is missing can still be useful only as a splitter.

Common mistakes

  • Only inserting balls next to their own colour, which misses boards like RRWWRRBBRR.
  • Clearing only one run per insert instead of repeating until no run of three is left.
  • Not sorting the hand, so the same balls in another order make new, repeated states.

How to talk through it

  1. 1Clarify. Do chain reactions keep going? Can the start board already have a run of three? Do I have to use every ball?
  2. 2Name the state. The board plus the sorted hand; the fewest moves to an empty board means BFS.
  3. 3Give the full search. Every ball at every gap with a memo; say it wastes most tries.
  4. 4Prune. Only next to the same colour, at the start of a run, or between two equal balls; explain why the split case is needed.
  5. 5Test out loud. Run an impossible board, a chain reaction and the split case before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?