단어 변환 문제 링크
from collections import deque
def solution(begin, target, words):
if target not in words:
return 0
dq = deque([(begin, 0)]) #* 시작 단어와 변한 횟수
while dq:
cur_word, stage = dq.popleft()
if cur_word == target:
return stage
for word in words:
if sum(c1 != c2 for c1, c2 in zip(cur_word, word)) == 1: #* 한 글자만 다른 경우
dq.append((word, stage + 1)) #* 다음 단계 단어와 변환 횟수 큐에 추가
words.remove(word) #* 이미 방문한 단어는 제거
return 0