forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0127.py
34 lines (29 loc) · 1.04 KB
/
0127.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
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
wordDict, step = set(wordList), 1
if endWord not in wordDict:
return 0
s1, s2 = set([beginWord]), set([endWord])
while s1:
stack = set([])
wordDict -= s1
for s in s1:
for i in range(len(beginWord)):
for j in string.ascii_lowercase:
tmp = s[:i] + j + s[i+1:]
if tmp not in wordDict:
continue
if tmp in s2:
return step + 1
stack.add(tmp)
if len(stack) < len(s2):
s1 = stack
else:
s1, s2 = s2, stack
step += 1
return 0
if __name__ == "__main__":
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
print(Solution().ladderLength(beginWord, endWord, wordList))