Why runtime error when it's correct in my local machine.


  • -1
    D
    class Solution {
    public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int distance = 1;
        unordered_set<string> s1,s2;
        s1.insert(start);
        unordered_set<string>::iterator it;
        int flag = 0;
        while(dict.size() > 0)
        {
            unordered_set<string>::iterator it2;
            for(it = dict.begin(); it != dict.end();)
            {
                flag = 0;
                for(it2 = s1.begin(); it2 != s1.end(); it2++)
                {
                    if(linked(*it, *it2))
                    {
                        s2.insert(*it);
                        dict.erase(it++);
                        flag = 1;
                    }
                }
                if(flag != 1)
                    it++;
            }
            for(unordered_set<string>::iterator ma = s2.begin(); ma != s2.end(); ma++)
            {
                if(linked(*ma, end))
                    return distance + 2;
            }
            if(s2.size() == 0)
                return -1;
            distance++;
            swap(s1,s2);
            s2.clear();
        }
        return -1;
    }
    
    
    bool linked(string str1, string str2)
    {
        int length = str1.length();
        int diff = 0;
        for(int i = 0; i < length; i++)
        {
            if(str1[i] != str2[i])
                diff++;
        }
        if(diff == 1)
            return true;
        return false;
    }
    

    };

    In my local machine ,the answer is right; why always says: runtime error
    Last executed input: "kiss", "tusk", ["miss","dusk","kiss","musk","tusk","diss","disk","sang","ties","muss"]


  • 0

    Please format your code correctly, the last }; is outside of the code area. Select the code you want to indent and press the {} button.


  • 0
    T

    Your code fails with seg fault, for the input you've mentioned in question. (on g++)


  • 0
    D

    I have found the problem, thank you very much .


Log in to reply
 

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