-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword-ladder.py
68 lines (49 loc) · 1.88 KB
/
word-ladder.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import queue
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
if endWord not in wordList:
return 0
wordList.append(beginWord)
begin_q = queue.Queue()
end_q = queue.Queue()
begin_q.put(beginWord)
end_q.put(endWord)
word_set = set(wordList)
FROM_BEGIN = 1
FROM_END = -1
NOT_VISITED = 0
# 字母表
CHAR_LIST = [chr(i) for i in range(97,123)]
word_dict = {word: NOT_VISITED for word in word_set}
word_dict[beginWord] = FROM_BEGIN
word_dict[endWord] = FROM_END
step = 2
cur_word = None
word_len = len(beginWord)
q = begin_q
flag = FROM_BEGIN
while not q.empty():
temp_q = queue.Queue()
while not q.empty():
cur_word = q.get()
for i in range(word_len):
for c in CHAR_LIST:
temp_word = cur_word[0: i] + c + cur_word[i + 1: word_len]
if temp_word in word_dict:
if word_dict[temp_word] == NOT_VISITED:
word_dict[temp_word] = flag
temp_q.put(temp_word)
elif word_dict[temp_word] != flag:
return step
step += 1
if flag == FROM_BEGIN:
begin_q = temp_q
else:
end_q = temp_q
if begin_q.qsize() <= end_q.qsize():
q = begin_q
flag = FROM_BEGIN
else:
q = end_q
flag = FROM_END
return 0