-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0127_Word_Ladder.py
More file actions
33 lines (27 loc) · 1018 Bytes
/
0127_Word_Ladder.py
File metadata and controls
33 lines (27 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
if not endWord in wordList:
return 0
visited = {}
def isNext(_curr, _next):
index = 0
while index < len(_curr):
if not _curr[index] == _next[index]:
return _curr[index+1:] == _next[index+1:]
index += 1
return True
queue = collections.deque([(beginWord, 0)])
while queue:
curr_str, step = queue.popleft()
if curr_str == endWord:
return step + 1
for word in wordList:
if word in visited:
visited[word] = min(visited[word], step + 1)
continue
if isNext(curr_str, word):
queue.append((word, step+1))
visited[word] = step+1
if endWord in visited:
return visited[endWord] + 1
return 0