← Back to challenges

Minimum Number of Genetic Mutations

PythonHardalgorithmslogicregexstrings

Instructions

A gene is represented by an 8-character long string containing: "A", "C", "G", "T". Gene mutation is defined as when a single character is changed in the gene string.

For example, "AACCGGTT" -> "AACCGGTA" is a mutation because only the last character is different.

Also, there is a list of genes, bank, which contains all the valid / allowed gene mutations.

The function is given start, end, and bank. Determine the minimum number of mutations needed to mutate from start to end. If there is no mutation chain found return -1.

Examples

min_mutations("AACCGGTT", "AACCGGTA", ["AACCGGTA"]) ➞ 1

min_mutations("AACCGGTT", "AAACGGTA", ["AACCGGTA", "AACCGCTA", "AAACGGTA"]) ➞ 2

min_mutations("AAAAACCC", "AACCCCCC", ["AAAACCCC", "AAACCCCC", "AACCCCCC"]) ➞ 3

min_mutations("AAAAACCC", "AACTCCCC", ["AAAACCCC", "AAACCCCC", "AACTCCCC"]) ➞ -1

Notes

  • The starting point is valid, so it might not be included in the bank. The endpoint is included.
  • All mutations in the searched sequence must be found in the bank.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.