MCPcopy
hub / github.com/HuberTRoy/leetCode / ladderLength

Method ladderLength

BFS/WordLadder.py:56–87  ·  view source on GitHub ↗

:type beginWord: str :type endWord: str :type wordList: List[str] :rtype: int

(self, beginWord, endWord, wordList)

Source from the content-addressed store, hash-verified

54from collections import deque
55class Solution(object):
56 def ladderLength(self, beginWord, endWord, wordList):
57 """
58 :type beginWord: str
59 :type endWord: str
60 :type wordList: List[str]
61 :rtype: int
62 """
63
64 if len(beginWord) == 1:
65 return 2
66
67 _wordList = set(wordList)
68
69 result = deque([[beginWord, 1]])
70
71 while result:
72 word, length = result.popleft()
73
74 if word == endWord:
75 return length
76
77 _length = length + 1
78
79 for i in range(len(word)):
80 for c in 'qwertyuiopasdfghjklzxcvbnm':
81 new = word[:i]+c+word[i+1:]
82
83 if new in _wordList:
84 _wordList.remove(new)
85 result.append([new, _length])
86
87 return 0

Callers

nothing calls this directly

Calls 1

removeMethod · 0.80

Tested by

no test coverage detected