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.

 

Example 1:

Input: board = "WRRBBW", hand = "RB"
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW

Example 2:

Input: board = "WWRRBBWW", hand = "WRBRW"
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty

Example 3:

Input: board = "G", hand = "GGGGG"
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty 

Example 4:

Input: board = "RBYYBBRRB", hand = "YRBGB"
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty 

 

Constraints:


Solution:

class Solution {
    Map<String, Integer> map = new HashMap();
    
    public int findMinStep(String board, String hand) {
        int min = helper("^" + board + "$", hand);
        // System.out.println(map);
        return min == Integer.MAX_VALUE ? -1 : min;
    }
    
    public int helper(String board, String hand) {
        // System.out.println(board);
        String hash = board + "#" + hand;
        if (map.containsKey(hash)) return map.get(hash);
        int min = Integer.MAX_VALUE;
        
        // System.out.println("before: " + board);

        // WWBBBWW
        // RRWWWRRBBBRR
        Deque<int[]> stack = new ArrayDeque();
        stack.push(new int[]{board.charAt(0), 1});
        int lastRemoved = -1;
        for (int i = 1; i < board.length(); i ++) {            
            int curr = board.charAt(i);
            while (stack.peekFirst()[0] != curr) {
                if (stack.peekFirst()[1] >= 3) {
                    lastRemoved = stack.peekFirst()[0];
                    stack.pop();
                }
                if (stack.peekFirst()[0] != curr) {
                    stack.push(new int[]{curr, 0});
                }
            }
            stack.peekFirst()[1] ++;
            if (stack.peekFirst()[1] >= 3) {
                lastRemoved = stack.peekFirst()[0];
                stack.pop();
            }
        }
        
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            int[] curr = stack.pop();
            for (int i = 0; i < curr[1]; i ++) {
                sb.append((char) curr[0]);
            }
        }
        board = sb.reverse().toString();
        
        boolean flag = true;
        for (int j = 1; j < board.length() - 1; j ++) {
            if (board.charAt(j) != lastRemoved) {
                flag = false;
            }
        }
        if (flag) board = "^$";
        
        // System.out.println("after: " + board);
                
        if (board.equals("^$"))  {
            min = 0;
            map.put(hash, min);
            return 0;
        }
    
        char prev = board.charAt(0);
        for (int i = 1; i < board.length(); i ++) {
            int h = hand.indexOf(prev);
            if (h != -1) {
                String newBoard = board.substring(0, i) + String.valueOf(prev) + board.substring(i);
                StringBuilder newHand = new StringBuilder(hand);
                newHand.deleteCharAt(h);
                int sub = helper(newBoard, newHand.toString());
                if (sub != Integer.MAX_VALUE) {
                    min = Math.min(min, 1 + sub);
                }
            }
            prev = board.charAt(i);
        }
        
        map.put(hash, min);
        return min;
    }
}