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 start to a gene string end where one mutation is defined as one single character changed in the gene string.


	For example, "AACCGGTT" --&gt; "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 start and end and the gene bank bank, return the minimum number of mutations needed to mutate from start to end. 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.

&nbsp;
Example 1:

Input: start = "AACCGGTT", end = "AACCGGTA", bank = ["AACCGGTA"]
Output: 1


Example 2:

Input: start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]
Output: 2


Example 3:

Input: start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC","AAACCCCC","AACCCCCC"]
Output: 3


&nbsp;
Constraints:


	start.length == 8
	end.length == 8
	0 &lt;= bank.length &lt;= 10
	bank[i].length == 8
	start, end, and bank[i] consist of only the characters ['A', 'C', 'G', 'T'].


class Solution:
    def minMutation(self, start: str, end: str, bank: List[str]) -> int:
        q = deque()
        q.append(start)
        n = len(bank)
        last = 0
        used = [False] * n
        for i, x in enumerate(bank):
            if start == x:
                used[i] = True
            if end == x:
                last = i
        dist = 0
        while q:
            dist += 1
            for _ in range(len(q)):
                w = q.popleft()
                for i, x in enumerate(bank):
                    if used[i]:
                        continue
                    bad = 0
                    for j in range(8):
                        if w[j] != x[j]:
                            bad += 1
                            if bad == 2:
                                break
                    if bad == 1:
                        if last == i:
                            return dist
                        used[i] = True
                        q.append(x)
        return -1
class Solution {
    public int minMutation(String start, String end, String[] bank) {
        Set<String> set = new HashSet<>();
        for(String tmp: bank){
            set.add(tmp);
        }
        if(!set.contains(end)) return -1;
        if(start.equals(end)) return 0;
        char[] var = {'A','C','G','T'};
        Queue<String> q = new LinkedList<>();
        q.add(start);
        int count = 0;
        while(!q.isEmpty()){
            int size = q.size();
            for(int i = 0; i < size; i ++){
                String str = q.poll();
                char[] tmp = str.toCharArray();
                if(str.equals(end)) return count;
                for(int j = 0; j < 8; j ++){
                    char ch = tmp[j];
                    for(int k = 0; k < 4; k ++){
                        tmp[j] = var[k];
                        String node = new String(tmp);
                        if(set.contains(node)){
                            q.add(node);
                            set.remove(node);
                        }
                    }
                    tmp[j] = ch;
                }
            }
            count++;
        }
        return -1;
    }
}
class Solution {
public:
    int minMutation(string start, string end, vector<string>& bank) {
        unordered_set<string> st(bank.begin(), bank.end());
        if (st.find(end) == st.end()) return -1;
        
        queue<string> q;
        q.push(start);
        
        unordered_set<string> vis;
        vis.insert(start);
        
        int res=0;
        while (not q.empty()) {
            int sz = q.size();
            
            while (sz--) {
                string currString = q.front(); q.pop();
                if (currString == end) return res;
                
                for (int i=0;i<currString.size();i++) {
                    string temp = currString;
                    for (auto g: string("ATGC")) {
                        temp[i] = g;
                        if (st.find(temp) != st.end() and (vis.find(temp) == vis.end())) {
                            q.push(temp);
                            vis.insert(temp);
                        }
                    }
                }
            }
            res++;
        }
        
        return -1;
    }
};
/**
 * @param {string} start
 * @param {string} end
 * @param {string[]} bank
 * @return {number}
 */
var minMutation = function(start, end, bank) {
    if(start.length!=end.length || !contains(end,bank)){
            return -1;
        }
        return bfs(start,end,bank);
    };
    function contains(end,bank){
        for(const x of bank){
            if(x==end){
                return true;
            }
        }
        return false;
    }
    function convertPossible(a,b){
        if(a.length != b.length) return false;
        let count=0;
        for(let i=0;i<a.length;i++){
            if(a[i]!=b[i]){
                count++;
            }
        }
        return count==1;
    }
    function bfs(start,end,bank){
        let count=0;
        let q=[];
        let set=new Set();
        q.push(start);

        while(q.length>0){
            let size=q.length;

            for(let i=0;i<size;i++){
                let curr=q.shift();

                if(curr==end){
                    return count;
                }
                bank.forEach((x)=>{
                    if(convertPossible(curr,x) && !set.has(x)){
                        q.push(x);
                        set.add(x);
                    }
                })
            }
            count++;
        }
        return -1;
    }
Minimum Genetic Mutation