Runtime error for testcase: "hot", "dog", ["hot","dog","dot"]. But working fine on my machine. Kindly help


  • 0
    S
    class Solution {
    public:
        int helper(string a, string b)
        {
            if(a.size()!= b.size()) return 0;
            int n= a.size(),c=0;
            for(int i=0;i<n;i++)
            {
                if(a[i]!= b[i]) c++;
            }
            if(c==1) return 1;
            return 0;
        }
        int ladderLength(string bw, string ew, unordered_set<string>& wd) {
            int i,j;
            int breadth=1;
            unordered_set<string>:: iterator it;
            if(wd.find(bw)!= wd.end()) wd.erase(wd.find(bw));
            vector<string> bfs;
            bfs.push_back(bw);
            int flag=0;
            bfs.push_back("delimiter");
            while(bfs.size()>1)
            {
                if(bfs[0]=="delimiter"){ bfs.erase(bfs.begin());breadth++;bfs.push_back("delimiter");}
                if(helper(bfs[0],ew)==1) {flag=1;break;}
                for(it=wd.begin();it!=wd.end();it++)
                {
                    if(helper(bfs[0],*it)==1)
                    {
                        //try interchange
                        bfs.push_back(*it);
                        wd.erase(it);
                    }
                }
                bfs.erase(bfs.begin());
            }
            if(flag==0) return 0;
            return breadth +1;
            
        }
    };

  • 0
    I

    Hot and Dog must not be there in the dictionary itself.


Log in to reply
 

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