I don't know why my code cannot pass this case


  • 0
    W

    I have used BFS to solve this problem by two queue.I don't know why OJ cannot pass this specific case which can be passed on my own machine.

    Input:	"a", "c", ["a","b","c"]
    Output:	1
    Expected:	2
    My code   
    
    
    
    class Solution {
        public:
            int ladderLength(string start, string end, unordered_set<string> &dict) {
                if((start.size()==0)||(start.size()!=end.size()))
                {
                    return 0;
                }
                int minLen = 0;
                queue<string>popone,pushone;
                popone.push(start);
                while(dict.size()>0&&!popone.empty())
                {
                    while(!popone.empty())
                    {
                        string str = popone.front();
                        popone.pop();
                        for(int i=0;i<str.size();i++)
                        {
                            char tmp = str[i];
                            for(char j='a';j<='z';j++)
                            {
                                if(j==tmp)
                                {
                                    continue;
                                }
                                str[i]=j;
                                if(str==end)
                                {
                                    minLen+=1;
                                    return minLen;
                                }
                                if(dict.find(str)!=dict.end())
                                {
                                    dict.erase(str);
                                    pushone.push(str);
                                }
                            }
                            str[i]=tmp;
                        }
                    }
                    while(!pushone.empty())
                    {
                        popone.push(pushone.front());
                        pushone.pop();
                    }
                    minLen++;
                }
                return 0;
            }
        };

  • 0
    C

    According to the problem description, the whole path includes from beginning to end. Thus a -> c should be included in the whole transformation path. As the length of the words is one, it should be successful all the time. The path length is 2.


Log in to reply
 

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