-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution2.py
31 lines (30 loc) · 1.1 KB
/
Solution2.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
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
def extend(m1, m2, q):
for _ in range(len(q)):
s = q.popleft()
step = m1[s]
s = list(s)
for i in range(len(s)):
ch = s[i]
for j in range(26):
s[i] = chr(ord('a') + j)
t = ''.join(s)
if t in m1 or t not in words:
continue
if t in m2:
return step + 1 + m2[t]
m1[t] = step + 1
q.append(t)
s[i] = ch
return -1
words = set(wordList)
if endWord not in words:
return 0
q1, q2 = deque([beginWord]), deque([endWord])
m1, m2 = {beginWord: 0}, {endWord: 0}
while q1 and q2:
t = extend(m1, m2, q1) if len(q1) <= len(q2) else extend(m2, m1, q2)
if t != -1:
return t + 1
return 0