Skip to content

Commit 8d61ddd

Browse files
author
hangyeol
committed
104차 3번 문제풀이
1 parent 98e562e commit 8d61ddd

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from collections import deque
2+
3+
def isAdjacent(word1, word2):
4+
diff_count = 0
5+
for a, b in zip(word1, word2):
6+
if a != b:
7+
diff_count += 1
8+
if diff_count > 1:
9+
return False
10+
return diff_count == 1
11+
12+
13+
def solution(begin, target, words):
14+
if target not in words:
15+
return 0
16+
17+
visited = set()
18+
queue = deque()
19+
queue.append((begin, 0)) # 현재 단어, count 수
20+
21+
while queue:
22+
current, depth = queue.popleft()
23+
if current == target:
24+
return depth
25+
26+
for word in words:
27+
if word not in visited and isAdjacent(current, word):
28+
visited.add(word)
29+
queue.append((word, depth + 1))
30+
31+
return 0

0 commit comments

Comments
 (0)