class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
wordSet = set(wordList)
seen = set([beginWord])
if endWord not in wordSet:
return(0)
dq = deque([beginWord])
level = 1
resp = float("inf")
while dq:
levelSize = len(dq)
for _ in range(levelSize):
currWord = dq.popleft()
currWordList = [c for c in currWord]
for cIdx, c in enumerate(currWordList):
for otherChar in string.ascii_lowercase:
potentialNextWordList = currWordList[:]
potentialNextWordList[cIdx] = otherChar
potentialNextWord = "".join(potentialNextWordList)
if potentialNextWord == endWord:
resp = min(resp, level + 1)
if potentialNextWord in wordSet and potentialNextWord not in seen:
seen.add(potentialNextWord)
dq.append(potentialNextWord)
level += 1
return(resp if resp != float("inf") else 0)