Skip to content
Decoder

Minimum Genetic Mutation

Medium

Problem

A gene is a string of 8 characters, each one of A, C, G or T. A mutation changes exactly one character of a gene. You are given a start gene, an end gene and a bank of allowed genes. Every gene you mutate into must be in the bank (start itself does not have to be). Return the fewest mutations that turn start into end, 0 if they are already equal, or -1 if it cannot be done.

Example 1

Input
start = "TTGGCCAA", end = "TTGGCCAC", bank = ["TTGGCCAC"]
Output
1

Example 2

Input
start = "GGGGGGGG", end = "TTGGGGGG", bank = ["TGGGGGGG", "TTGGGGGG", "GTGGGGGG"]
Output
2

Example 3

Input
start = "AACCTTGG", end = "AACCTTGA", bank = []
Output
-1

Constraints

  • 0 <= bank.length <= 10
  • start, end and every bank[i] have length 8.
  • Every gene contains only the characters A, C, G and T.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.