C++ Solution with deque(0ms)


  • 0
    M
    class Solution {
    public:
    int minMutation(string start, string end, vector<string>& bank) {
        deque<string> toVisit;
        toVisit.push_back(start);
        int currentDistance = 0;
        while(toVisit.size() > 0){
            int currentLength = toVisit.size();
            for(int i=0; i<currentLength; i++){
                string current = toVisit.front();
                toVisit.pop_front();
                if(stringDistance(current, end) == 0){
                        return currentDistance;
                    }
                for(int i=0; i<bank.size(); i++){
                    if(stringDistance(current, bank[i]) == 1){
                        toVisit.push_back(bank[i]);
                        swap(bank[i], bank.back());
                        bank.pop_back();
                        i--;
                    }
                }
            }
            currentDistance++;
        }
        return -1;
    }
    
    int stringDistance(const string &s1, const string &s2){
        int distance=0;
        for(int i=0; i<s1.size();i++){
            if(s1[i]!=s2[i]){
                distance++;
            }
        }
        return distance;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.