Sponsored
Sponsored
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.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.
1import java.util.*;
2
3public class GeneticMutation {
4 public int minMutation(String startGene, String endGene, String[
In this Java implementation, BFS helps in finding the minimal mutation using a queue and a set for storing the genes from the bank. The gene strings are processed by character substitution.
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.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.
1#include <iostream>
2#include <unordered_set>
3#include <vector>
using namespace std;
int minMutation(string startGene, string endGene, vector<string>& bank) {
unordered_set<string> geneBank(begin(bank), end(bank));
if (!geneBank.count(endGene)) return -1;
unordered_set<string> forward = {startGene};
unordered_set<string> backward = {endGene};
int steps = 0;
vector<char> choices = {'A', 'C', 'G', 'T'};
while (!forward.empty() && !backward.empty()) {
if (forward.size() > backward.size())
swap(forward, backward);
unordered_set<string> nextLevel;
for (const string& gene : forward) {
string currentGene = gene;
for (int i = 0; i < currentGene.size(); ++i) {
char original = currentGene[i];
for (char c : choices) {
currentGene[i] = c;
if (backward.find(currentGene) != backward.end())
return steps + 1;
if (geneBank.find(currentGene) != geneBank.end()) {
nextLevel.insert(currentGene);
geneBank.erase(currentGene);
}
}
currentGene[i] = original;
}
}
swap(forward, nextLevel);
++steps;
}
return -1;
}
int main() {
vector<string> bank = {"AACCGGTA", "AACCGCTA", "AAACGGTA"};
cout << minMutation("AACCGGTT", "AAACGGTA", bank) << endl;
return 0;
}
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.