-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lv2-단어 변환.py
40 lines (27 loc) · 903 Bytes
/
Lv2-단어 변환.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
from collections import deque
def bfs(begin, target, words):
size = len(begin)
queue = deque([(begin, 0)])
while queue:
word, step = queue.popleft()
if word == target:
return step
for compare_word, compare_step in words:
diff = 0
for i in range(size):
if diff > 1:
break
if word[i] != compare_word[i]:
diff += 1
if diff == 1:
words.remove((compare_word, compare_step))
queue.append((compare_word, step + 1))
return 0
def solution(begin, target, words):
words = list(zip(words, [0] * len(words)))
return bfs(begin, target, words)
if __name__ == '__main__':
begin = "hit"
target = "cog"
words = ["hot", "dot", "dog", "lot", "log", "cog"]
print(solution(begin, target, words))