Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
A gene string can be represented by an 8-character long string, with choices from 'A', 'C', 'G', and 'T'.
Suppose we need to investigate a mutation from a gene string startGene to a gene string endGene where one mutation is defined as one single character changed in the gene string.
"AACCGGTT" --> "AACCGGTA" is one mutation.There is also a gene bank bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string.
Given the two gene strings startGene and endGene and the gene bank bank, return the minimum number of mutations needed to mutate from startGene to endGene. If there is no such a mutation, return -1.
Note that the starting point is assumed to be valid, so it might not be included in the bank.
Example 1:
Input: startGene = "AACCGGTT", endGene = "AACCGGTA", bank = ["AACCGGTA"] Output: 1
Example 2:
Input: startGene = "AACCGGTT", endGene = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"] Output: 2
Constraints:
0 <= bank.length <= 10startGene.length == endGene.length == bank[i].length == 8startGene, endGene, and bank[i] consist of only the characters ['A', 'C', 'G', 'T'].Problem summary: A gene string can be represented by an 8-character long string, with choices from 'A', 'C', 'G', and 'T'. Suppose we need to investigate a mutation from a gene string startGene to a gene string endGene where one mutation is defined as one single character changed in the gene string. For example, "AACCGGTT" --> "AACCGGTA" is one mutation. There is also a gene bank bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string. Given the two gene strings startGene and endGene and the gene bank bank, return the minimum number of mutations needed to mutate from startGene to endGene. If there is no such a mutation, return -1. Note that the starting point is assumed to be valid, so it might not be included in the bank.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"AACCGGTT" "AACCGGTA" ["AACCGGTA"]
"AACCGGTT" "AAACGGTA" ["AACCGGTA","AACCGCTA","AAACGGTA"]
word-ladder)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #433: Minimum Genetic Mutation
class Solution {
public int minMutation(String startGene, String endGene, String[] bank) {
Deque<String> q = new ArrayDeque<>();
q.offer(startGene);
Set<String> vis = new HashSet<>();
vis.add(startGene);
int depth = 0;
while (!q.isEmpty()) {
for (int m = q.size(); m > 0; --m) {
String gene = q.poll();
if (gene.equals(endGene)) {
return depth;
}
for (String next : bank) {
int c = 2;
for (int k = 0; k < 8 && c > 0; ++k) {
if (gene.charAt(k) != next.charAt(k)) {
--c;
}
}
if (c > 0 && !vis.contains(next)) {
q.offer(next);
vis.add(next);
}
}
}
++depth;
}
return -1;
}
}
// Accepted solution for LeetCode #433: Minimum Genetic Mutation
func minMutation(startGene string, endGene string, bank []string) int {
type pair struct {
s string
depth int
}
q := []pair{pair{startGene, 0}}
vis := map[string]bool{startGene: true}
for len(q) > 0 {
p := q[0]
q = q[1:]
if p.s == endGene {
return p.depth
}
for _, next := range bank {
diff := 0
for i := 0; i < len(startGene); i++ {
if p.s[i] != next[i] {
diff++
}
}
if diff == 1 && !vis[next] {
vis[next] = true
q = append(q, pair{next, p.depth + 1})
}
}
}
return -1
}
# Accepted solution for LeetCode #433: Minimum Genetic Mutation
class Solution:
def minMutation(self, startGene: str, endGene: str, bank: List[str]) -> int:
q = deque([(startGene, 0)])
vis = {startGene}
while q:
gene, depth = q.popleft()
if gene == endGene:
return depth
for nxt in bank:
diff = sum(a != b for a, b in zip(gene, nxt))
if diff == 1 and nxt not in vis:
q.append((nxt, depth + 1))
vis.add(nxt)
return -1
// Accepted solution for LeetCode #433: Minimum Genetic Mutation
use std::collections::{HashSet, VecDeque};
impl Solution {
pub fn min_mutation(start_gene: String, end_gene: String, bank: Vec<String>) -> i32 {
let mut q = VecDeque::new();
q.push_back((start_gene.clone(), 0));
let mut vis = HashSet::new();
vis.insert(start_gene);
while let Some((gene, depth)) = q.pop_front() {
if gene == end_gene {
return depth;
}
for next in &bank {
let mut c = 2;
for k in 0..8 {
if gene.as_bytes()[k] != next.as_bytes()[k] {
c -= 1;
}
if c == 0 {
break;
}
}
if c > 0 && !vis.contains(next) {
vis.insert(next.clone());
q.push_back((next.clone(), depth + 1));
}
}
}
-1
}
}
// Accepted solution for LeetCode #433: Minimum Genetic Mutation
function minMutation(startGene: string, endGene: string, bank: string[]): number {
const q: [string, number][] = [[startGene, 0]];
const vis = new Set<string>([startGene]);
for (const [gene, depth] of q) {
if (gene === endGene) {
return depth;
}
for (const next of bank) {
let c = 2;
for (let k = 0; k < 8 && c > 0; ++k) {
if (gene[k] !== next[k]) {
--c;
}
}
if (c && !vis.has(next)) {
q.push([next, depth + 1]);
vis.add(next);
}
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.