classSolution(object):defladderLength(self, beginWord, endWord, wordList):"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
dict1={}for w in wordList:ifnot w in dict1:
dict1[w]=1else:
dict1[w]+=1
q=[(beginWord,1)]while q:
t,l = q.pop(0)if t == endWord:return l
for i inrange(len(t)):
left = t[:i]
right = t[i+1:]for s in'abcdefghijklmnopqrstuvwxyz':if s!=t[i]:
nextWord = left+s+right
if nextWord in dict1 and dict1[nextWord]>0:
q.append((nextWord,l+1))
dict1[nextWord]-=1return0