Skip to content

Minimum Genetic Mutation

A gene string can be represented by an 8-character long string, with choices from 'A', 'C', 'G', and 'T'.

Suppose we need to investigate a mutation from a startGene to an endGene where one mutation is defined as one single character changed in the gene string.

  • There is a bank of valid gene mutations. Only mutations in the bank are allowed.
  • Return the minimum number of mutations needed to mutate from startGene to endGene. If there is no such mutation sequence, return -1.
startGeneendGenebankOutputExplanation
"AACCCCCC""AACCCCTA"["AACCCCTA"]1Direct mutation from start to end in one step.
"AACCCCCC""AAACCCTA"["AACCCCTA","AACCCCCA","AAACCCTA","AAACCCCA","AAACCCTA"]2Path: AACCCCCC → AACCCCTA → AAACCCTA
"AAAAAAAA""CCCCCCCC"["AAAAAAAC","AAAAAACA","AAAAACC","AAAAACCC","AAAACCCC","AACACCCC","ACCACCCC","ACCCCCCC","CCCCCCCC"]4Multiple valid mutation paths exist.
"AACCCCCC""AACCCCCB"["AACCCCTA"]-1End gene not in bank or unreachable.
  • startGene.length == 8
  • endGene.length == 8
  • 0 <= bank.length <= 10
  • bank[i].length == 8
  • startGene, endGene, and bank[i] consist of only the characters 'A', 'C', 'G', and 'T'.
  • startGene != endGene
ApproachTimeSpaceNotes
BFSO(n)O(n)First approach
DFSO(n)O(n)Second approach
★ Recommended
⏱ Time O(n) 💾 Space O(n)
minimum_genetic_mutation_bfs.py
from collections import deque
def minMutation(startGene, endGene, bank):
"""BFS to find minimum mutations."""
if endGene not in bank:
return -1
bank_set = set(bank)
queue = deque([(startGene, 0)])
visited = {startGene}
chars = ['A', 'C', 'G', 'T']
while queue:
gene, mutations = queue.popleft()
if gene == endGene:
return mutations
for i in range(len(gene)):
for char in chars:
if char != gene[i]:
new_gene = gene[:i] + char + gene[i+1:]
if new_gene in bank_set and new_gene not in visited:
visited.add(new_gene)
queue.append((new_gene, mutations + 1))
return -1
✓ Simple
⏱ Time O(n) 💾 Space O(n)
minimum_genetic_mutation_dfs.py
def minMutation(startGene, endGene, bank):
"""DFS with memoization to find minimum mutations."""
if endGene not in bank:
return -1
bank_set = set(bank)
memo = {}
chars = ['A', 'C', 'G', 'T']
def dfs(gene):
if gene == endGene:
return 0
if gene in memo:
return memo[gene]
result = float('inf')
for i in range(len(gene)):
for char in chars:
if char != gene[i]:
new_gene = gene[:i] + char + gene[i+1:]
if new_gene in bank_set:
result = min(result, 1 + dfs(new_gene))
memo[gene] = result
return result
ans = dfs(startGene)
return ans if ans != float('inf') else -1