-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path126-word-ladder-ii.py
49 lines (46 loc) · 1.64 KB
/
126-word-ladder-ii.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from collections import defaultdict, deque
import string
class Solution:
def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
graph = defaultdict(list)
wordset = set(wordList)
queue = deque([beginWord])
while queue:
length = len(queue)
next_queue = set()
remove_wordset = set()
found = False
for _ in range(length):
word = queue.popleft()
if word == endWord:
found = True
break
for replace_i in range(len(word)):
for c in string.ascii_lowercase:
new_word = word[: replace_i] + c + word[replace_i + 1:]
if new_word in wordset:
next_queue.add(new_word)
remove_wordset.add(new_word)
graph[new_word].append(word)
if found:
break
wordset -= remove_wordset
queue.extend(next_queue)
res = []
def dfs(path):
if path[- 1] == beginWord:
res.append(path[:: - 1])
return
for neighbor in graph[path[- 1]]:
path.append(neighbor)
dfs(path)
path.pop()
dfs([endWord])
return res
# time O(n * L**2 + nL * 26**L), n * L**2 for bfs and building graph
# space O(nL), not couning output
# using graph and bfs with hashset as queue and hashset and dfs and backtracking
'''
1. bfs to build graph
2. dfs and backtracking to get all paths
'''