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
Gene Mutation GraphAACCGGTTSTARTAACCGGTA1 mutAACCGCTA2 mutAAACGGTATARGETAACCGGTGinvalidT→AC→AG→CinvalidBFS Algorithm WalkthroughLevel 0: [AACCGGTT] - Start gene in queueLevel 1: [AACCGGTA] - Single mutation, valid in bankLevel 2: [AAACGGTA] - Target found! Minimum mutations = 2✅ Path: AACCGGTT → AACCGGTA → AAACGGTA (2 mutations)Why BFS Works• Explores level by level• First target = minimum path• Unweighted graph property
Understanding the Visualization
1
Model as Graph
Each valid gene string is a node. Edges connect genes differing by one character.
2
Apply BFS
Use BFS to explore from startGene, guaranteeing shortest path discovery.
3
Generate Neighbors
For each gene, try changing each position to each nucleotide (32 possibilities).
4
Validate & Queue
Only queue neighbors that exist in bank and haven't been visited.
Key Takeaway
🎯 Key Insight: Gene mutation is a shortest path problem in an unweighted graph. BFS naturally finds the minimum number of mutations by exploring paths level by level.
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