def solution(begin, target, words):
b = len(begin)
if target not in words:
return 0
if begin in words:
words.remove(begin)
words.remove(target)
words.append(target)
## 차이가 1만큼 나는 단어들
allword = [begin] + words
allword = allword[::-1]
diff1 = {i:[] for i in allword}
for i in range(len(allword)):
k1 = list(allword[i])
for j in range(i+1,len(allword)):
dif = 0
k2 = list(allword[j])
for kk in range(b):
if k1[kk] != k2[kk]:
dif += 1
if dif > 1:
break
if dif == 1:
diff1[allword[i]].append(allword[j])
answer = 1
start = diff1[target] #[log,dog] []
while begin not in start:
now = []; answer += 1
for i in start:
if begin in diff1[i]:
return answer
now += diff1[i]
start = list(set(now))
return answer