Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. class Solution(object):
  2. def ladderLength(self, beginWord, endWord, wordList):
  3. wordList = set(wordList)
  4. queue = collections.deque([[beginWord, 1]])
  5. while queue:
  6. word, length = queue.popleft()
  7. if word == endWord:
  8. return length
  9. for i in range(len(word)):
  10. for c in 'abcdefghijklmnopqrstuvwxyz':
  11. next_word = word[:i] + c + word[i+1:]
  12. if next_word in wordList:
  13. wordList.remove(next_word)
  14. queue.append([next_word, length + 1])
  15. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement