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.

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.

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

n
2n
โœ“ Linear Growth
Space Complexity
O(8 * N)

Recursion stack depth up to 8*N in worst case, plus visited set

n
2n
โœ“ 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']
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