# Minimum Genetic Mutation LeetCode Solution

Here, We see Minimum Genetic Mutation LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

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.

• For example, “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

## Minimum Genetic Mutation LeetCode Solution C++

``````class Solution {
public:
int minMutation(string startGene, string endGene, vector<string>& bank) {
queue<string> q;
unordered_map<string, int> vis;
int steps = 0;
q.push(startGene);
vis[startGene] = 1;
while (!q.empty()) {
for (int i = q.size(); i > 0; i--) {
string s = q.front();
q.pop();
if (s == endGene) return steps;
for (int j = 0; j < 8; j++) {
char oc = s[j];
for (int k = 0; k < 4; k++) {
s[j] = "ACGT"[k];
if (!vis[s] && find(bank.begin(), bank.end(), s) != bank.end()) {
q.push(s);
vis[s] = 1;
}
}
s[j] = oc;
}
}
steps += 1;
}
return -1;
}
};```Code language: C++ (cpp)```

## Minimum Genetic Mutation LeetCode Solution Java

``````class Solution {
public int minMutation(String startGene, String endGene, String[] bank) {
Queue<String> q = new LinkedList<>();
HashSet<String> vis = new HashSet<String>();
List<String> banks = Arrays.asList(bank);
int steps = 0;
while (!q.isEmpty()) {
for (int i = q.size(); i > 0; i--) {
String s = q.poll();
if (s.equals(endGene)) return steps;
char[] ca = s.toCharArray();
for (int j = 0; j < 8; j++) {
char oc = ca[j];
for (int k = 0; k < 4; k++) {
ca[j] = "ACGT".charAt(k);
String t = new String(ca);
if (!vis.contains(t) && banks.contains(t)) {
}
}
ca[j] = oc;
}
}
steps += 1;
}
return -1;
}
}```Code language: Java (java)```

## Minimum Genetic Mutation Solution JavaScript

``````var minMutation = function(startGene, endGene, bank) {
const choices = ['A', 'C', 'G', 'T'];
const queue = [startGene];
const seen = new Set([startGene]);
let steps = 0;
while (queue.length !== 0) {
const nodesInQueue = queue.length;
for (let j = 0; j < nodesInQueue; j++) {
const node = queue.shift();
if (node === endGene)
return steps;
for (const choice of choices) {
for (let i = 0; i < node.length; i++) {
const neighbor = node.substring(0, i) + choice + node.substring(i + 1);
if (!seen.has(neighbor) && bank.includes(neighbor)) {
queue.push(neighbor);
}
}
}
}
steps++;
}
return -1;
}```Code language: JavaScript (javascript)```

## Minimum Genetic Mutation Solution Python

``````class Solution(object):
def minMutation(self, startGene, endGene, bank):
queue, seen = deque([(startGene, 0)]), {startGene}
while queue:
s, n = queue.popleft()
if s == endGene: return n
for i in range(8):
for ch in 'ACGT':
m = s[:i]+ch+s[i+1:]
if m in bank and m not in seen: