-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0433-minimum-genetic-mutation.py
More file actions
28 lines (23 loc) · 832 Bytes
/
0433-minimum-genetic-mutation.py
File metadata and controls
28 lines (23 loc) · 832 Bytes
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
# time complexity: O(b)
# space complexity: O(1)
from collections import deque
from typing import List
class Solution:
def minMutation(self, startGene: str, endGene: str, bank: List[str]) -> int:
queue = deque([(startGene, 0)])
seen = {startGene}
while queue:
node, steps = queue.popleft()
if node == endGene:
return steps
for c in "ACGT":
for i in range(len(node)):
neighbor = node[:i] + c + node[i+1:]
if neighbor in bank and neighbor not in seen:
queue.append((neighbor, steps + 1))
seen.add(neighbor)
return -1
startGene = "AACCGGTT"
endGene = "AACCGGTA"
bank = ["AACCGGTA"]
print(Solution().minMutation(startGene, endGene, bank))