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'].In this approach, we utilize the Breadth-First Search algorithm to explore the mutation process. The problem can be transformed into finding the shortest path in an unweighted graph (where nodes are gene strings and edges represent valid transformations as per the gene bank).
Steps involved:
startGene.endGene is reached, return the mutation count.endGene, return -1.This C program uses a queue to perform a BFS search. It starts from the startGene and attempts to form every possible valid mutation step by step using the genes in the bank. A visited array prevents cycles.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N * M * 4) where N is the number of steps in bank, M is length of startGene (8), and 4 is from trying each nucleotide.
Space Complexity: O(N) for the queue.
This approach aims to optimize the BFS by conducting simultaneous searches from both the start and end genes. This reduces the search space effectively.
Steps:
startGene and endGene, respectively.This C++ solution implements bidirectional BFS to find the shortest mutation path by expanding the smaller frontier set of genes in each step, improving efficiency.
Python
Time Complexity: O(N * M * 4), with quicker terminations due to bidirectional checks.
Space Complexity: O(N) for two sets tracking forward and backward layers.
| Approach | Complexity |
|---|---|
| Breadth-First Search (BFS) Approach | Time Complexity: O(N * M * 4) where N is the number of steps in bank, M is length of startGene (8), and 4 is from trying each nucleotide. Space Complexity: O(N) for the queue. |
| Bidirectional BFS Approach | Time Complexity: O(N * M * 4), with quicker terminations due to bidirectional checks. Space Complexity: O(N) for two sets tracking forward and backward layers. |
Minimum Genetic Mutation -(Asked by Twitter) : Explanation ➕ Live Coding • codestorywithMIK • 36,544 views views
Watch 9 more video solutions →Practice Minimum Genetic Mutation with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor