The answer is correct in my IDE, but wrong answer when submitted to OJ


  • 0
    G
    private boolean isConnected(String A, String B) {
            int sum = 0;
            for (int i = 0; i < A.length(); i++) {
                if (A.charAt(i) != B.charAt(i)) {
                    sum++;
                    if (sum > 1) {
                        return false;
                    }
                }
            }
            return sum == 1;
        }
    public List<List<String>> findLadders2(String start, String end, Set<String> dict) {
            List<List<String>> outList = new ArrayList<>();
            List<List<String>> tempList = new ArrayList<>();
            List<List<String>> tempList2 = new ArrayList<>();
            Set<String> tempDict = new HashSet<>();
            Set<String> excludeFilters = new HashSet<>();
    
            List<String> t1 = new ArrayList<>();
            t1.add(start);
            tempList.add(t1);
            int minPathLength = 0;
            boolean foundPath = false;
            while (tempList.size() > 0) {
                while (tempList.size() > 0) {
                    List<String> currentList = tempList.remove(0);
                    String lastElement = currentList.get(currentList.size() - 1);
                    if (foundPath == false) {
                        if (isConnected(lastElement, end)) {
                            foundPath = true;
                            minPathLength = currentList.size() + 1;
                            currentList.add(end);
                            outList.add(currentList);
                        } else {
                            HashSet<String> foundNeighbours = findNeighbours(lastElement, dict, tempDict, excludeFilters);
                            for (String neighbour : foundNeighbours) {
                                List<String> newList = new ArrayList<>();
                                for (int k = 0; k < currentList.size(); k++) {
                                    newList.add(currentList.get(k));
                                }
                                newList.add(neighbour);
                                tempList2.add(newList);
                            }
                        }
                    } else {
                        if (currentList.size() == minPathLength - 1 && isConnected(lastElement, end)) {
                            currentList.add(end);
                            outList.add(currentList);
                        }
                    }
                }
                for (List<String> t2 : tempList2) {
                    tempList.add(t2);
                }
                tempList2.clear();
                tempDict.clear();
            }
            return outList;
        }
    
        private HashSet<String> findNeighbours(String start, Set<String> dict, Set<String> tempDict, Set<String> excludeFilters) {
            HashSet<String> outSet = new HashSet<>();
            if (dict.contains(start)) {
                dict.remove(start);
            }
            for (int i = 0; i < start.length(); i++) {
                char[] allchars = start.toCharArray();
                allchars[i] = '*';
                String filterString = new String(allchars);
                if (!excludeFilters.contains(filterString)) {
                    excludeFilters.add(filterString);
                    for (int j = 0; j < 26; j++) {
                        allchars[i] = (char) ((int) 'a' + j);
                        String candidateString = new String(allchars);
                        if (tempDict.contains(candidateString)) {
                            outSet.add(candidateString);
                        } else if (dict.contains(candidateString)) {
                            outSet.add(candidateString);
                            dict.remove(candidateString);
                            tempDict.add(candidateString);
                        }
                    }
                }
            }
            return outSet;
        }
    

    Sorry my code is long. But I got the right answer in my netbeans but wrong in OJ for test case Input: "qa", "sq", ["si","go","se","cm","so","ph","mt","db","mb","sb","kr","ln","tm","le","av","sm","ar","ci","ca","br","ti","ba","to","ra","fa","yo","ow","sn","ya","cr","po","fe","ho","ma","re","or","rn","au","ur","rh","sr","tc","lt","lo","as","fr","nb","yb","if","pb","ge","th","pm","rb","sh","co","ga","li","ha","hz","no","bi","di","hi","qa","pi","os","uh","wm","an","me","mo","na","la","st","er","sc","ne","mn","mi","am","ex","pt","io","be","fm","ta","tb","ni","mr","pa","he","lr","sq","ye"]


Log in to reply
 

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