# leetcode 488. 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.

Examples:
Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty

Note:

1. 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.
2. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
3. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
4. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.

Input: "RRWWRRBBRR", "WB". The test program gave the expected answer -1. However, I thought the answer might be 2. Because:

RRWWRRBBRR -> RRWWRRBBR[W]R -> RRWWRRBB[B]RWR -> RRWWRRRWR -> RRWWWR -> RRR -> empty

public int minSteps(String board,int[] hands){
board=removeDuplicate3(board);
if(board.length()==0){
return 0;
}
char[] cs=board.toCharArray();
int min=6;//因为The number of balls in your hand won't exceed 5
int low=0;
int high=1;
int duplicate=1;
while(low<cs.length){
if(high>=cs.length||cs[high]!=cs[low]){
int need=3-duplicate;
if(hands[cs[low]-'A']>=need){
hands[cs[low]-'A']-=need;
String subStr="";
if(high>=cs.length){
subStr=board.substring(0,low);
}
else{
subStr=board.substring(0,low)+board.substring(high);
}
int step=need+minSteps(subStr, hands);
if(step<min){
min=step;
}
hands[cs[low]-'A']+=need;
}
low=high;
high=low+1;
duplicate=1;
}
else{
high++;
duplicate++;
}
}
return min;
}

public String removeDuplicate3(String board){
boolean isFind=true;
String result="";
while(isFind==true){
isFind=false;
if(board.length()<3){
return board;
}
result="";
int low=0;
int fast=1;
int duplicate=1;
//之所以不让fast<board.length()
//是考虑到BYYBBRRB这个末尾有一个独立的字母的情况
while(low<board.length()){
while(fast<board.length()&&board.charAt(fast)==board.charAt(low)){
fast++;
duplicate++;
}
if(duplicate<3){
for(int i=low;i<fast;i++){
result+=board.charAt(i);
}
}
else{
isFind=true;
}
low=fast;
fast=low+1;
duplicate=1;
}
board=result;
}
return result;
}

It's just like a DFS or a Backtracking solution. word is poor, just look the code.

int MAXCOUNT = 6;   // the max balls you need will not exceed 6 since "The number of balls in your hand won't exceed 5"

public int findMinStep(String board, String hand) {
int[] handCount = new int[26];
for (int i = 0; i < hand.length(); ++i) ++handCount[hand.charAt(i) - 'A'];
int rs = helper(board + "#", handCount);  // append a "#" to avoid special process while j==board.length, make the code shorter.
return rs == MAXCOUNT ? -1 : rs;
}
private int helper(String s, int[] h) {
s = removeConsecutive(s);
if (s.equals("#")) return 0;
int  rs = MAXCOUNT, need = 0;
for (int i = 0, j = 0 ; j < s.length(); ++j) {
if (s.charAt(j) == s.charAt(i)) continue;
need = 3 - (j - i);     //balls need to remove current consecutive balls.
if (h[s.charAt(i) - 'A'] >= need) {
h[s.charAt(i) - 'A'] -= need;
rs = Math.min(rs, need + helper(s.substring(0, i) + s.substring(j), h));
h[s.charAt(i) - 'A'] += need;
}
i = j;
}
return rs;
}
//remove consecutive balls longer than 3
private String removeConsecutive(String board) {
for (int i = 0, j = 0; j < board.length(); ++j) {
if (board.charAt(j) == board.charAt(i)) continue;
if (j - i >= 3) return removeConsecutive(board.substring(0, i) + board.substring(j));
else i = j;
}
return board;
}

huanghanqian

• 擅长领域：
• Java
• MySQL
• JavaScript
• React
• J2EE