Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def ladderLength(self, beginWord, endWord, wordList):
- """
- :type beginWord: str
- :type endWord: str
- :type wordList: List[str]
- :rtype: int
- """
- if endWord not in wordList:
- return 0
- queue = []
- queue.append((beginWord, 1))
- visited = set()
- while len(queue)>0:
- currword, currlvl = queue.pop(0)
- if currword == endWord:
- return currlvl
- for word in wordList:
- if word not in visited:
- if self.checkifonediff(currword, word):
- visited.add(word)
- queue.append((word, currlvl + 1))
- return 0
- def checkifonediff(self, cword, word):
- count = 0
- for i in range(len(cword)):
- if cword[i] != word[i]:
- count += 1
- if count != 1:
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement