Skip to content

Commit b312d2a

Browse files
authored
Create Minimum Genetic Mutation.py
1 parent e9814a4 commit b312d2a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

medium/Minimum Genetic Mutation.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#433. Minimum Genetic Mutation
2+
class Solution:
3+
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
4+
queue = deque([(start, 0)])
5+
seen = {start}
6+
7+
while queue:
8+
node, steps = queue.popleft()
9+
if node == end:
10+
return steps
11+
12+
for c in "ACGT":
13+
for i in range(len(node)):
14+
neighbor = node[:i] + c + node[i + 1:]
15+
if neighbor not in seen and neighbor in bank:
16+
queue.append((neighbor, steps + 1))
17+
seen.add(neighbor)
18+
19+
return -1
20+

0 commit comments

Comments
 (0)