# 단어 변환

단어 변환 - 프로그래머스

from collections import deque

def solution(begin, target, words):
    d = dict()
    d[begin] = 0
    q = deque([begin])

    while q:
        cur = q.popleft()
        for word in words:
            cnt = 0
            for i, j in zip(word, cur):
                if i != j:
                    cnt += 1
                if cnt == 2:
                    break
            if cnt == 1 and word not in d:
                q.append(word)
                d[word] = d[cur] + 1
    return d.get(target, 0)