2
def findMinStep(self, board: str, hand: str) -> int:4
# start from i and remove continues ball10
while left > 0 and s[left - 1] == s[i]:12
while right + 1 < len(s) and s[right + 1] == s[i]:15
length = right - left + 117
new_s = s[:left] + s[right + 1 :]18
return remove_same(new_s, left - 1)22
hand = "".join(sorted(hand))24
# board, hand and step25
q = collections.deque([(board, hand, 0)])26
visited = set([(board, hand)])29
curr_board, curr_hand, step = q.popleft()30
for i in range(len(curr_board) + 1):31
for j in range(len(curr_hand)):32
# skip the continue balls in hand33
if j > 0 and curr_hand[j] == curr_hand[j - 1]:36
# only insert at the begin of continue balls in board38
i > 0 and curr_board[i - 1] == curr_hand[j]39
): # left side same color43
# 1. same color with right44
# 2. left and right are same but pick is different45
if i < len(curr_board) and curr_board[i] == curr_hand[j]:48
0 < i < len(curr_board)49
and curr_board[i - 1] == curr_board[i]50
and curr_board[i] != curr_hand[j]55
new_board = remove_same(56
curr_board[:i] + curr_hand[j] + curr_board[i:], i58
new_hand = curr_hand[:j] + curr_hand[j + 1 :]61
if (new_board, new_hand) not in visited:62
q.append((new_board, new_hand, step + 1))63
visited.add((new_board, new_hand))