Minimum Genetic Mutation

Medium#433states

A gene is a string of 8 letters, each A, C, G or T. One mutation changes one letter, and only genes from a given bank are allowed along the way; the start gene does not have to be in the bank. Find the fewest mutations from the start gene to the end gene, or -1 if it cannot be done. Our example: start AAGGTTCC, end AAGCTTCA, bank [AAGCTTCA, TAGGTTCC, AAGGTTCA] — the answer is 2.

input
startGene and endGene: genes, strings of 8 letters A, C, G, T; bank: the genes a mutation may make.
returns
The fewest mutations that turn startGene into endGene. A mutation changes one letter of the gene, and the gene it makes must be in bank; startGene need not be there. -1 if there is no way.
constraints
  • 0 ≤ bank.length ≤ 10
  • startGene, endGene and bank[i] are 8 letters long, each of A, C, G, T
  • startGene ≠ endGene
our 14 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} startGene
 * @param {string} endGene
 * @param {string[]} bank
 * @return {number}
 */
function minMutation(startGene, endGene, bank) {

}
Ctrl ↵
format
reset

saved in this browser

14 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more breadth-first search (bfs) problems

Pattern “Breadth-first search (BFS)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.