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.
1using System;
2using System.Collections.Generic;
3
4public class GeneticMutation {
5 public int MinMutation(string startGene, string endGene, string[] bank) {
6 var geneBank = new HashSet<string>(bank);
7 if (!geneBank.Contains(endGene)) return -1;
8
9 var choices = new char[] {'A', 'C', 'G', 'T'};
10 var queue = new Queue<(string gene, int steps)>();
11 queue.Enqueue((startGene, 0));
12
13 while (queue.Count > 0) {
var (gene, steps) = queue.Dequeue();
if (gene == endGene) return steps;
for (int i = 0; i < gene.Length; i++) {
var geneArray = gene.ToCharArray();
char original = gene[i];
foreach (var choice in choices) {
if (choice == original) continue;
geneArray[i] = choice;
string newGene = new string(geneArray);
if (geneBank.Contains(newGene)) {
geneBank.Remove(newGene);
queue.Enqueue((newGene, steps + 1));
}
}
geneArray[i] = original;
}
}
return -1;
}
public static void Main() {
var gm = new GeneticMutation();
string startGene = "AACCGGTT";
string endGene = "AAACGGTA";
string[] bank = {"AACCGGTA", "AACCGCTA", "AAACGGTA"};
Console.WriteLine(gm.MinMutation(startGene, endGene, bank));
}
}
The C# code solution uses a BFS implemented with a queue. It checks mutations by iterating over gene positions and attempting character replacements, using a set for quick lookup.
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.