https://leetcode.com/problems/zuma-game/

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.

Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.

Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.

1 | Examples: |

Note:

- You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
- The number of balls on the table won’t exceed 20, and the string represents these balls is called “board” in the input.
- The number of balls in your hand won’t exceed 5, and the string represents these balls is called “hand” in the input.
- Both input strings will be non-empty and only contain characters ‘R’,’Y’,’B’,’G’,’W’.

#### java

https://discuss.leetcode.com/topic/75434/recursive-java-solution

Recursive java solution

1 | public class Solution { |

https://discuss.leetcode.com/topic/75447/search-and-memorize-java-solution

Search and memorize Java solution

Just searching and memorizing…

It is not a good interview question because many codes are not related to the algorithm.

1 | public class Solution { |

https://discuss.leetcode.com/topic/75460/java-bfs-solution

Java BFS solution

For each iteration, we find all the possible ways to insert a ball according to the current state.

1 | public class Solution { |

#### cpp

https://discuss.leetcode.com/topic/75465/concise-3ms-c-solution

Concise 3ms C++ Solution

At the first look, it is similar to Can I Win. But not like that problem, the board state is not relevant to the state of hand and this problem requires the minimum value. So, it seems that memorization might not help much. So, I just do it brute-force and stop iff it is sure there no better results than current one.

Sort hand string so that we easily know if there at least 2 balls with same color in hand to eliminate a single ball on board. Refer to inline comments for details.

According to the description, the number of balls in hand won’t exceed 5, to make life easier, I just return a number equal or greater than 6 when there is no way to clear board.

1 | #define MAX_STEP 6 |

https://discuss.leetcode.com/topic/75448/memoization-c-solution

memoization c++ solution

1 | class Solution { |