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:
Your mission: Transform a
Example:
However, there's a catch! ๐งฌ Not all mutations are biologically valid. You have a
Goal: Return the minimum mutations needed, or
Note: The starting gene is always considered valid, even if not in the bank.
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.
Visualization
Tap to expand
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.
Time & Space Complexity
Time Complexity
O(4^8 * N)
In worst case, we might explore 4^8 possible gene combinations, checking each against bank of size N
โ Linear Growth
Space Complexity
O(8 * N)
Recursion stack depth up to 8*N in worst case, plus visited set
โ Linear Space
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']
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code