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:

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

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Dynamic programming

Graph traversal

Starter code: class Solution:
    def findMinStep(self, board: str, hand: str) -> int:
        

Solutions:
class Solution:

	def findMinStep(self, board, hand):
		res = float('inf')
		hmap = collections.defaultdict(int)
		for c in hand:
			hmap[c] += 1
		res = self.helper(board, hmap)
		if res == float('inf'):
			return -1
		return res

	def helper(self, board, hmap):
		board = self.removeConsecutive(board)
		if len(board) == 0:
			return 0
		cnt = float('inf')
		j = 0
		for i in range(len(board) + 1):
			if i < len(board) and board[i] == board[j]:
				continue
			need = 3 - (i - j)
			if hmap[board[j]] >= need:
				hmap[board[j]] -= need
				res = self.helper(board[0:j] + board[i:], hmap)
				if res != float('inf'):
					cnt = min(cnt, res + need)
				hmap[board[j]] += need
			j = i
		return cnt

	def removeConsecutive(self, board):
		j = 0
		for i in range(len(board) + 1):
			if i < len(board) and board[i] == board[j]:
				continue
			if i - j >= 3:
				return self.removeConsecutive(board[0:j] + board[i:])
			else:
				j = i
		return board


class Solution:

	def findMinStep(self, board, hand):
		return self.use_dfs(board, hand)

	def use_dfs(self, board, hand):
		counts = collections.Counter(hand)
		return self.dfs(board, counts)

	def dfs(self, board, hand):
		if not board:
			return 0
		result = float('inf')
		(i, j) = (0, 0)
		n = len(board)
		while i < n:
			j = i
			while j < n and board[i] == board[j]:
				j += 1
			color = board[i]
			insert = 3 - (j - i)
			if color in hand and hand[color] >= insert:
				new_board = self.shrink(board[:i] + board[j:])
				hand[color] -= insert
				dist = self.dfs(new_board, hand)
				if dist >= 0:
					result = min(result, dist + insert)
				hand[color] += insert
			i = j
		return result if float('inf') != result else -1

	def shrink(self, board):
		(i, j) = (0, 0)
		while i < len(board):
			j = i
			while j < len(board) and board[i] == board[j]:
				j += 1
			if j - i >= 3:
				board = board[:i] + board[j:]
				i = 0
			else:
				i = j
		return board