We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 98e562e commit 8d61dddCopy full SHA for 8d61ddd
1 file changed
live10/test104/문제3/백한결.py
@@ -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
0 commit comments