<p>You are playing a variation of the game Zuma.</p>

<p>In this variation of Zuma, there is a <strong>single row</strong> of colored balls on a board, where each ball can be colored red <code>&#39;R&#39;</code>, yellow <code>&#39;Y&#39;</code>, blue <code>&#39;B&#39;</code>, green <code>&#39;G&#39;</code>, or white <code>&#39;W&#39;</code>. You also have several colored balls in your hand.</p>

<p>Your goal is to <strong>clear all</strong> of the balls from the board. On each turn:</p>

<ul>
	<li>Pick <strong>any</strong> ball from your hand and insert it in between two balls in the row or on either end of the row.</li>
	<li>If there is a group of <strong>three or more consecutive balls</strong> of the <strong>same color</strong>, remove the group of balls from the board.
	<ul>
		<li>If this removal causes more groups of three or more of the same color to form, then continue removing each group until there are none left.</li>
	</ul>
	</li>
	<li>If there are no more balls on the board, then you win the game.</li>
	<li>Repeat this process until you either win or do not have any more balls in your hand.</li>
</ul>

<p>Given a string <code>board</code>, representing the row of balls on the board, and a string <code>hand</code>, representing the balls in your hand, return <em>the <strong>minimum</strong> number of balls you have to insert to clear all the balls from the board. If you cannot clear all the balls from the board using the balls in your hand, return </em><code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> board = &quot;WRRBBW&quot;, hand = &quot;RB&quot;
<strong>Output:</strong> -1
<strong>Explanation:</strong> It is impossible to clear all the balls. The best you can do is:
- Insert &#39;R&#39; so the board becomes WRR<u>R</u>BBW. W<u>RRR</u>BBW -&gt; WBBW.
- Insert &#39;B&#39; so the board becomes WBB<u>B</u>W. W<u>BBB</u>W -&gt; WW.
There are still balls remaining on the board, and you are out of balls to insert.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> board = &quot;WWRRBBWW&quot;, hand = &quot;WRBRW&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> To make the board empty:
- Insert &#39;R&#39; so the board becomes WWRR<u>R</u>BBWW. WW<u>RRR</u>BBWW -&gt; WWBBWW.
- Insert &#39;B&#39; so the board becomes WWBB<u>B</u>WW. WW<u>BBB</u>WW -&gt; <u>WWWW</u> -&gt; empty.
2 balls from your hand were needed to clear the board.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> board = &quot;G&quot;, hand = &quot;GGGGG&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> To make the board empty:
- Insert &#39;G&#39; so the board becomes G<u>G</u>.
- Insert &#39;G&#39; so the board becomes GG<u>G</u>. <u>GGG</u> -&gt; empty.
2 balls from your hand were needed to clear the board.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= board.length &lt;= 16</code></li>
	<li><code>1 &lt;= hand.length &lt;= 5</code></li>
	<li><code>board</code> and <code>hand</code> consist of the characters <code>&#39;R&#39;</code>, <code>&#39;Y&#39;</code>, <code>&#39;B&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;W&#39;</code>.</li>
	<li>The initial row of balls on the board will <strong>not</strong> have any groups of three or more consecutive balls of the same color.</li>
</ul>

class Solution:
    def findMinStep(self, board: str, hand: str) -> int:
        
        # Function to remove consecutive balls of the same color that appear 3 or more times
        def remove_consecutive_balls(s):
            i = 0
            while i < len(s):
                j = i
                # Find the end of the current segment of the same color
                while j < len(s) and s[j] == s[i]:
                    j += 1
                # Remove the segment if it has 3 or more consecutive balls
                if j - i >= 3:
                    return remove_consecutive_balls(s[:i] + s[j:])
                i = j
            return s
        
        # Depth-First Search function to determine the minimum steps to clear the board
        def dfs(board_state, hand_count):
            board_state = remove_consecutive_balls(board_state)
            # If the board is empty, no more steps needed
            if not board_state:
                return 0
            # If no balls are left in hand, it's impossible to clear the board
            if not any(hand_count.values()):
                return float('inf')
            
            min_steps = float('inf')
            i = 0
            while i < len(board_state):
                j = i
                # Find the end of the current segment of the same color
                while j < len(board_state) and board_state[j] == board_state[i]:
                    j += 1
                # Calculate how many more balls of the same color are needed
                needed_balls = 3 - (j - i)
                # If enough balls are available, try to use them
                if hand_count[board_state[i]] >= needed_balls:
                    hand_count[board_state[i]] -= needed_balls
                    min_steps = min(min_steps, needed_balls + dfs(board_state[:i] + board_state[j:], hand_count))
                    hand_count[board_state[i]] += needed_balls
                i = j
            return min_steps
        
        # Function to try inserting each color from hand into every possible position in the board
        def try_inserting_balls(board_state, hand_count):
            min_steps = float('inf')
            # Try inserting each ball into every position in the board
            for i in range(len(board_state) + 1):
                for color in hand_count:
                    if hand_count[color] > 0:
                        new_board = board_state[:i] + color + board_state[i:]
                        hand_count[color] -= 1
                        min_steps = min(min_steps, 1 + dfs(new_board, hand_count))
                        hand_count[color] += 1
            return min_steps
        
        # Initialize hand ball count
        hand_ball_count = Counter(hand)
        # Get the minimum steps to clear the board
        result = try_inserting_balls(board, hand_ball_count)
        return result if result != float('inf') else -1

16700