LeetCode Problem

How to Solve Minimum Genetic Mutation

Use a breadth-first search (BFS) to explore all valid one-character mutations from the start gene. Maintain a hash table of valid genes from the bank for constant-time checks. Track mutation steps layer by layer until reaching the end gene or exhausting possibilities.

GhostInterview Help

Need help with Minimum Genetic Mutation without spending extra time grinding it?

GhostInterview can read Minimum Genetic Mutation from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #433Hash Table plus StringReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Hash Table plus String
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Use a breadth-first search (BFS) to explore all valid one-character mutations from the start gene. Maintain a hash table of valid genes from the bank for constant-time checks. Track mutation steps layer by layer until reaching the end gene or exhausting possibilities.

Problem Statement

You are given two 8-character gene strings, startGene and endGene, containing only 'A', 'C', 'G', and 'T'. Each mutation changes exactly one character. Only genes present in the provided bank are valid mutations.

Determine the minimum number of mutations needed to transform startGene into endGene. Return -1 if the transformation is impossible. Each step must produce a gene from the bank, and repeated states are invalid.

Examples

Example 1

Input: startGene = "AACCGGTT", endGene = "AACCGGTA", bank = ["AACCGGTA"]

Output: 1

Example details omitted.

Example 2

Input: startGene = "AACCGGTT", endGene = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]

Output: 2

Example details omitted.

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'].

Solution Approach

Breadth-First Search Traversal

Perform BFS starting from startGene. At each step, generate all possible single-character mutations and enqueue only those present in the hash table of valid genes. Track levels to count mutations.

Hash Table Validation

Store all genes in the bank in a hash set for O(1) lookup. Before enqueuing a mutated gene, check its presence in the hash table to ensure only valid genes are processed.

Early Termination and State Management

Immediately return the current step count when endGene is reached. Maintain a visited set to prevent revisiting genes, avoiding cycles that cause infinite loops or incorrect mutation counts.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(8 * 4^8) in the worst case due to generating all one-character mutations, but limited by bank size and BFS pruning. Space complexity is O(bank.length + queue size) for hash table and BFS queue.

What Interviewers Usually Probe

  • Expect BFS usage due to minimal step requirement.
  • Hash table for fast gene validation is crucial.
  • Consider edge cases where endGene is not reachable from startGene.

Common Pitfalls or Variants

Common pitfalls

  • Mutating genes not present in the bank leading to invalid paths.
  • Failing to mark visited genes, causing cycles.
  • Counting steps incorrectly by not tracking BFS levels.

Follow-up variants

  • Allow multiple simultaneous mutations per step, requiring a modified BFS.
  • Genes of variable length, requiring dynamic mutation generation.
  • Large bank sizes, emphasizing memory-efficient hash table or pruning strategies.

How GhostInterview Helps

  • Highlights BFS pattern with hash table validation for mutation problems.
  • Prevents revisiting invalid gene states to avoid common pitfalls.
  • Provides step counting guidance layer by layer to ensure minimal mutation tracking.

Topic Pages

FAQ

What is the main pattern used in Minimum Genetic Mutation?

The problem combines a hash table for fast gene validation and BFS to explore minimal mutation paths efficiently.

How do I handle genes not in the bank?

Ignore any mutation not present in the hash table to ensure only valid gene sequences are considered.

Can mutations revert a previously changed character?

No, previously visited genes should be tracked to prevent cycles; each mutation sequence must be unique.

What determines the BFS step count?

Each BFS level corresponds to one mutation step; reaching endGene at level k means k mutations.

What if startGene equals endGene?

Return 0 mutations immediately since no changes are needed.

GhostInterview Solver

Need direct help with Minimum Genetic Mutation instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Genetic Mutation from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.