target이 words에 아예 없으면 0을 반환한다
BFS를 활용해서 target과 다른 알파벳이 1개만 있는 경우라면 q에 append해서 cnt를 늘려가며 비교한다
from collections import deque
def solution(begin, target, words):
answer = 0
if target not in words:
return 0
q = deque()
q.append((begin, 0))
while q:
word, cnt = q.popleft()
if word==target:
answer = cnt
break
for i in range(len(words)):
diff = 0
for j in range(len(words[i])):
if word[j]!=words[i][j]:
diff += 1
if diff==1:
q.append((words[i], cnt+1))
return answer