Minimum Genetic Mutation
Minimum Genetic Mutation is LeetCode 433 (Medium). A gene is an 8-character string of the letters A, C, G and T, and one mutation changes exactly one character. Return the fewest mutations that turn startGene into endGene, where every gene reached along the way, endGene included, must be in bank. Return -1 if it cannot be done.
startGeneitself does not have to be in the bank.- The bank holds at most 10 genes, so the search is always small.
- 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'].
Intuition
Treat each gene as a node, with an edge between two genes that differ in exactly one character. Every mutation crosses one edge, so the answer is the shortest path from startGene to endGene, and BFS finds shortest paths when every edge counts as 1.
BFS works in rounds: first every gene one mutation away, then every gene two away, and so on. So the round in which endGene comes out of the queue is the minimum number of mutations.
The edges are never built in advance. From a gene, try the other 3 letters at each of the 8 positions, and keep only the results that are in the gene bank and not seen yet.
"Fewest single changes from one string to another, through a list of allowed strings" is BFS on an implicit graph: the strings are nodes and a one-character change is an edge. Here the alphabet is only 4 letters long and every string is 8 characters.
Approach
Before reading on, list every gene you can reach in one mutation from a given gene, and how many there are. Then decide what the search must remember so it never expands the same gene twice. Aim for a BFS that touches each bank gene at most once.
Put the bank in a set
Store the bank genes in a hash set so each lookup is O(1). If endGene is not in it, return -1 at once: the last mutation must land on a bank gene, so the target can never be reached.
Start from the start gene
Queue startGene, mark it as seen and set steps = 0. The start counts as zero mutations, and marking it seen stops the search from walking back to it.
Take one round at a time
Pop every gene that was in the queue when the round began. If one of them is endGene, return steps, because BFS reaches every gene in the fewest rounds possible.
Try every one-letter change
For each of the 8 positions and each letter in ACGT, build the changed gene. If it is in the bank and not seen, mark it seen and queue it for the next round. Marking it as it is queued means no gene is ever queued twice.
Count the round, or give up
After each round add 1 to steps and continue with the genes just queued. If the queue runs empty first, no chain of bank genes leads to endGene, so return -1.
Code in Python, C++ and Java
Common pitfalls
Requiring the start gene to be in the bank
if startGene not in genes or endGene not in genes:
return -1if endGene not in genes:
return -1Only the genes you mutate into must be in the bank. On the problem's first example, startGene = "AACCGGTT" with bank = ["AACCGGTA"], the extra check returns -1, but the answer is 1.
Counting the start gene as one step
queue = deque([startGene]) steps = 1
queue = deque([startGene]) steps = 0
This problem counts mutations, the changes between genes, not the genes in the chain. Starting at 1 returns 2 on that same first example, where one mutation is enough.
Searching without a seen set
if nxt in genes:
queue.append(nxt)if nxt in genes and nxt not in seen:
seen.add(nxt)
queue.append(nxt)Two bank genes one change apart keep queuing each other. For startGene = "AACCGGTT", endGene = "TTTTTTTT", bank = ["AACCGGTA", "AACCGCTA", "TTTTTTTT"], the target is unreachable and the queue never empties, so the loop never ends.
Edge cases
If endGene is in the bank, the first gene popped is the target and the answer is 0.
Complexity
Minimum Genetic Mutation vs Word Ladder
LeetCode 127 is the same BFS on a larger scale. The differences are all in the details:
| Problem | Letters tried | Counts | No path | Size |
|---|---|---|---|---|
| 433 Minimum Genetic Mutation (this page) | 4: A C G T | mutations, so start at 0 | return -1 | at most 10 genes of length 8 |
| 127 Word Ladder | 26: a to z | words in the sequence, so start at 1 | return 0 | up to 5,000 words of length 10 |
Minimum Genetic Mutation FAQ
Can I compare each gene with every bank gene instead of generating changes?
Yes. Two genes are one mutation apart when exactly one of their 8 characters differs, and with at most 10 bank genes that check costs almost nothing. Generating the 24 changes and looking them up in a set scales better when the bank is large, as in Word Ladder.