Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
5
Indexable
class Solution {
public:
    int minMutation(string start, string end, vector<string>& bank) {
        unordered_set<string>st{bank.begin(),bank.end()};  //.find() returns iterator index whereas .count() returns bool for unordered_set
        if(!st.count(end)) return -1;
        int count=0;
        queue<string> q;
        q.push(start);
        while(!q.empty()){
            int s=q.size();
            while(s--){
                string t=q.front();
                q.pop();
                if(t==end) return count;
                st.erase(t); //this is same as marking it visited
                
                for(int i=0;i<8;i++){
                    string f=t;
                    f[i]='A';
                    if(st.count(f)) q.push(f);
                    f[i]='C';
                    if(st.count(f)) q.push(f);
                    f[i]='G';
                    if(st.count(f)) q.push(f);
                    f[i]='T';
                    if(st.count(f)) q.push(f);
                }
            }
            count++;
        }
        return -1;
    }
};