Wrong code got accepted. More test cases needed?


  • 0

    I thought endWord is in wordList so my code is wrong yet it still got accepted. My code cannot even give correct answer for the exemple given in the description of this problem but it still got accepted. Is there something wrong with the test cases?

     public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
            if (wordList.size() == 0) return 0;
            int length = 1;
            Queue<String> que = new LinkedList<>();
            que.add(beginWord);
            int levelNum = 1;
            int nextLevelNum = 0;
            while (!que.isEmpty()) {
                char[] temp = que.poll().toCharArray();
    
                for (int i = 0; i < temp.length; i++) {
                    char c = temp[i];
                    for (char j = 'a'; j <= 'z'; j++) {
                        if (j == c) continue;
                        temp[i] = j;
                        String s = String.valueOf(temp);
                        if (wordList.contains(s)) {
                        \\ only return when endWord is in wordList
                            if (s.equals(endWord)) return length + 1;
                            que.add(s);
                            wordList.remove(s);
                            nextLevelNum++;
                        }
                        temp[i] = c;
                    }
                }
    
                levelNum--;
                if (levelNum == 0) {
                    levelNum = nextLevelNum;
                    nextLevelNum = 0;
                    length++;
                }
    
            }
            return 0;
        }
    

Log in to reply
 

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