Testcase "a" -> "a"


  • 0
    W

    I was thinking about the test case is not complete.
    Say we have start and end word is the same, "a",
    so the output should be 1.
    But it looks like my solution get accepted which will generate 2.

        class Solution {
    public:
        int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
            unordered_map<string, int> map;
            map[beginWord] = 1;
            wordDict.insert(endWord);
            queue<string> q;
            q.push(beginWord);
            while(!q.empty()) {
                string cur = q.front();
                q.pop();
                int lvl = map[cur];
                for(int i = 0; i < cur.length(); i++) {
                    for(char c = 'a'; c <= 'z'; c++) {
                        string tmp = cur;
                        tmp[i] = c;
                        if(wordDict.count(tmp)) {
                            if(tmp.compare(endWord) == 0)
                            return lvl+1;
                            if(map.count(tmp) == 0) {
                                map[tmp] = lvl+1;
                                q.push(tmp);
                            }   
                        }
                    }
                }
            }
            return 0;
        }
    };

Log in to reply
 

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