Minimum Genetic Mutation - Problem
Genetic Evolution Simulator

Imagine you're a geneticist studying DNA mutations! You have a gene string represented by an 8-character sequence using only nucleotides: 'A', 'C', 'G', and 'T'.

Your mission: Transform a startGene into an endGene through the minimum number of mutations. Each mutation changes exactly one character in the gene string.

Example: "AACCGGTT" → "AACCGGTA" is one mutation (T→A)

However, there's a catch! 🧬 Not all mutations are biologically valid. You have a bank of valid gene sequences that represents all possible intermediate steps. A gene must exist in this bank to be considered a valid mutation target.

Goal: Return the minimum mutations needed, or -1 if transformation is impossible.

Note: The starting gene is always considered valid, even if not in the bank.

Input & Output

example_1.py — Basic Transformation
$ Input: startGene = "AACCGGTT", endGene = "AACCGGTA", bank = ["AACCGGTA"]
Output: 1
💡 Note: Only one mutation needed: change the last 'T' to 'A'. The result "AACCGGTA" exists in the bank, so transformation is valid.
example_2.py — Multi-step Transformation
$ Input: startGene = "AACCGGTT", endGene = "AAACGGTA", bank = ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
Output: 2
💡 Note: Two mutations needed: AACCGGTT → AACCGGTA → AAACGGTA. Each intermediate step exists in the bank.
example_3.py — Impossible Transformation
$ Input: startGene = "AAAAACCC", endGene = "AACCCCCC", bank = ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
Output: 3
💡 Note: Three mutations needed: AAAAACCC → AAAACCCC → AAACCCCC → AACCCCCC. All steps are valid.

Constraints

  • 0 ≤ bank.length ≤ 10
  • startGene.length == endGene.length == bank[i].length == 8
  • startGene, endGene, and bank[i] consist of only the characters ['A', 'C', 'G', 'T']

Visualization

Tap to expand
Genetic Evolution Simulator - BFS Approach INPUT Start Gene: AACCGGTT End Gene: AACCGGTA Gene Bank: ["AACCGGTA"] Character Difference: A A C C G G T T A A C C G G T A Only position 8 differs (T --> A) ALGORITHM STEPS 1 Initialize BFS Queue: [(AACCGGTT, 0)] Visited: {AACCGGTT} 2 Process Current Dequeue: AACCGGTT Mutations: 0 3 Try All Mutations For each position, try A, C, G, T changes 4 Found Valid Mutation AACCGGTA in bank! Equals endGene - Done! BFS Tree: AACCGGTT AACCGGTA FINAL RESULT Mutation Path Found: AACCGGTT (Start) 1 mutation T --> A AACCGGTA (End) Output: 1 OK - Success! Minimum mutations: 1 Key Insight: BFS guarantees the shortest path (minimum mutations) because it explores all genes at distance k before exploring genes at distance k+1. Each valid mutation that exists in the bank is one step closer to the target. Time: O(n * L * 4) where n=bank size, L=gene length (8), 4=possible bases. TutorialsPoint - Minimum Genetic Mutation | BFS Approach
Asked in
Google 42 Amazon 35 Microsoft 28 Meta 22
42.8K Views
Medium-High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen