-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0126.py
More file actions
37 lines (35 loc) · 1.33 KB
/
0126.py
File metadata and controls
37 lines (35 loc) · 1.33 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
33
34
35
36
37
class Solution:
def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
wordList = set(wordList)
if endWord not in wordList: return []
queue = collections.deque([(beginWord, 1)])
graph = collections.defaultdict(set)
visited = set([beginWord])
distance = {}
while queue:
word, dist = queue.popleft()
distance[word] = dist
for i in range(len(word)):
for j in 'abcdefghijklmnopqrstuvwxyz':
if j != word[i]:
new = word[:i] + j + word[i+1:]
if new in wordList:
graph[new].add(word)
graph[word].add(new)
if new not in visited:
queue.append((new, dist+1))
visited.add(new)
tmp, res = [], []
def dfs(word):
tmp.append(word)
if word == endWord:
res.append(list(tmp))
tmp.pop()
return
if word in graph:
for v in graph[word]:
if distance[v] == distance[word]+1:
dfs(v)
tmp.pop()
dfs(beginWord)
return res