# Minimum Genetic Mutation in C++

C++Server Side ProgrammingProgramming

Suppose we have a gene string. That can be represented by a string whose length is 8, This string is consists of these letters [A, C, G, T]. Now consider we want to investigate about a mutation, where ONE mutation is actually ONE single character changed in the gene string. As an example, "AACCGTTT" is changed like "AACCGTTA" is 1 mutation.

We also have a given gene "bank", where all the valid gene mutations are present. A gene must be in the bank to make it a valid gene string.

Now suppose we have given 3 things - start, end, bank, our task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If conversion from start to end is not possible, then return -1.

So, if the input is like start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA", "AACCGCTA", "AAACGGTA"], then the output will be 2.

To solve this, we will follow these steps −

• Define a function putStar(), this will take s,

• Define an array ret

• for initialize i := 0, when i ≪ size of s, update (increase i by 1), do −

• temp := substring of s from 0 to i-1 concatenate " * " + substring of s from index i + 1 to end

• insert temp at the end of ret

• return ret

• From the main method do the following −

• Define one map called graph.

• for initialize i := 0, when i < size of bank, update (increase i by 1), do −

• s := bank[i]

• out = putStar(bank[i])

• for initialize j := 0, when j < size of out, update (increase j by 1), do

• insert s at the end of graph[out[j]]

• Define one queue q

• insert start into q

• Define one set visited

• insert start into visited

• for initialize lvl := 1, when not q is empty, update (increase lvl by 1), do −

• sz := size of q

• while sz is non-zero, decrease sz in each iteration, do −

• node := first element of q

• delete element from q

• out = putStar(node)

• for initialize i := 0, when i < size of out, update (increase i by 1), do−

• u := out[i]

• for initialize j := 0, when j < size of graph[u], update (increase j by 1), do −

• v := graph[u, j]

• if vi is in visited, then come out from the loop

• if v is same as end, then return lvl

• insert v into visited

• insert v into q

• return -1

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector <string> putStar(string s){
vector <string> ret;
for(int i = 0; i < s.size(); i++){
string temp = s.substr(0, i) + "*" + s.substr(i + 1);
ret.push_back(temp);
}
return ret;
}
int minMutation(string start, string end, vector<string>& bank) {
unordered_map < string, vector <string> > graph;
for(int i = 0; i < bank.size(); i++){
string s = bank[i];
vector <string> out = putStar(bank[i]);
for(int j = 0; j < out.size(); j++){
graph[out[j]].push_back(s);
}
}
queue <string> q;
q.push(start);
set <string> visited;
visited.insert(start);
for(int lvl = 1; !q.empty(); lvl++){
int sz = q.size();
while(sz--){
string node = q.front();
q.pop();
vector <string> out = putStar(node);
for(int i = 0; i < out.size(); i++){
string u = out[i];
for(int j = 0; j < graph[u].size(); j++){
string v = graph[u][j];
if(visited.count(v)) continue;
if(v == end) return lvl;
visited.insert(v);
q.push(v);
}
}
}
}
return -1;
}
};
main(){
Solution ob;
vector<string> v = {"AACCGGTA", "AACCGCTA", "AAACGGTA"};
cout << (ob.minMutation("AACCGGTT", "AAACGGTA", v));
}

## Input

"AACCGGTT", "AAACGGTA", {"AACCGGTA", "AACCGCTA", "AAACGGTA"}

## Output

2
Updated on 05-Jun-2020 07:01:54