488 Zuma Game

488. Zuma Game

1. Question

hink 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'.

2. Implementation

(1) DFS

class Solution {
    public int findMinStep(String board, String hand) {
        int[] count = new int[256];

        for (char c : hand.toCharArray()) {
            ++count[c];
        }

        return getMinStepByDFS(board, count);
    }

    public int getMinStepByDFS(String board, int[] count) {
        if (board.length() == 0) {
            return 0;
        }

        int res = Integer.MAX_VALUE;

        int i = 0;
        int j = 0;

        while (i < board.length()) {
            while (j < board.length() && board.charAt(i) == board.charAt(j)) {
                ++j;
            }

            char color = board.charAt(i);
            int balls = 3 - (j - i);

            if (count[color] >= balls) {
                String newBoard = collapse(board.substring(0, i) + board.substring(j));
                count[color] -= balls;

                int steps = getMinStepByDFS(newBoard, count);

                if (steps >= 0) {
                    res = Math.min(res, steps + balls);
                }

                count[color] += balls;
            }
            i = j;
        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }

    public String collapse(String board) {
        int i = 0;

        while (i < board.length()) {
            int j = i;
            while (j < board.length() && board.charAt(i) == board.charAt(j)) {
                ++j;
            }

            if (j - i >= 3) {
                board = board.substring(0, i) + board.substring(j);
                i = 0;
            }
            else {
                ++i;
            }
        }
        return board;
    }
}

3. Time & Space Complexity

DFS: 时间复杂度: O((m + n)^m * n^2), m是board的长度,n是hand的长度, 空间复杂度: O(mn)

4. References

https://www.youtube.com/watch?v=KVlbMB7gRIk

Last updated