Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def ladderLength(self, beginWord, endWord, wordList):
- wordList = set(wordList)
- queue = collections.deque([[beginWord, 1]])
- while queue:
- word, length = queue.popleft()
- if word == endWord:
- return length
- for i in range(len(word)):
- for c in 'abcdefghijklmnopqrstuvwxyz':
- next_word = word[:i] + c + word[i+1:]
- if next_word in wordList:
- wordList.remove(next_word)
- queue.append([next_word, length + 1])
- return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement