간단한 BFS문제
from collections import deque
def solution(begin, target, words):
answer = 0
check = dict()
for word in words:
check[word] = False
if target not in words:
return 0
mn = len(words) + 1
q = deque()
q.append((begin, 0))
while q:
curr, curr_count = q.popleft()
if curr == target:
return curr_count
for word in words:
count = 0
for i in range(len(word)):
if curr[i] == word[i]:
continue
else:
count += 1
if count > 1:
break
if count == 1:
if check[word] is False:
check[word] = True
q.append((word, curr_count + 1))
return 0