-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0126_Word_Ladder_II.py
More file actions
32 lines (30 loc) · 1.15 KB
/
Copy path0126_Word_Ladder_II.py
File metadata and controls
32 lines (30 loc) · 1.15 KB
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
class Solution:
def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
# improved BFS
# time complexity: O(N * M^2)
if endWord not in wordList:
return []
from collections import defaultdict
graph = defaultdict(set)
for word in wordList: # O(N)
for i in range(len(word)): # O(M)
graph[word[:i] + '*' + word[i + 1:]].add(word) # O(M)
from collections import deque
queue = deque()
queue.append((beginWord, [beginWord]))
visited = set()
min_len = float('inf')
ans = []
while queue: # O(N)
word, path = queue.popleft()
visited.add(word)
if len(path) > min_len:
continue
if word == endWord:
min_len = len(path)
ans.append(path)
for i in range(len(word)): # O(M)
for neighbor in graph[word[:i] + '*' + word[i + 1:]]: # O(M)
if neighbor not in visited:
queue.append((neighbor, path + [neighbor]))
return ans