-
Notifications
You must be signed in to change notification settings - Fork 481
/
0126.py
35 lines (32 loc) · 1.29 KB
/
0126.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
from collections import defaultdict
class Solution:
def findLadders(self, beginWord, endWord, wordList):
wordDict, res = set(wordList), []
if endWord not in wordDict:
return res
s1, s2, step = defaultdict(list), defaultdict(list), 2
s1[beginWord].append([beginWord])
s2[endWord].append([endWord])
while s1:
stack = defaultdict(list)
wordDict -= s1.keys()
for w, paths in s1.items():
for i in range(len(w)):
for j in string.ascii_lowercase:
tmp = w[:i] + j + w[i+1:]
if tmp not in wordDict:
continue
if tmp in s2:
if paths[0][0] == beginWord:
res += [f + b[::-1] for f in paths for b in s2[tmp]]
else:
res += [b + f[::-1] for f in paths for b in s2[tmp]]
stack[tmp] += [p + [tmp] for p in paths]
if len(stack) < len(s2):
s1 = stack
else:
s1, s2 = s2, stack
step += 1
if res and step > len(res[0]):
return res
return res