I got a curious error


  • 0
    W

    I got an error which wont occur on my eclipse.
    I dont know why.

    public class Solution {
        public List<List<String>> findLadders(String beginWord,String endWord,Set<String> wordList) {
            wordList.add(endWord);
            wordList.remove(beginWord);
            Queue<String> queue = new LinkedList<>();
            List<List<String>> result = new ArrayList<>();
            Set<String> set = new HashSet<>(wordList);
            Map<String,Integer> map = new HashMap<>();
            for(String word : wordList) map.put(word,Integer.MAX_VALUE);
            int level = 1;
            boolean found = false;
            queue.offer(beginWord);
            while(!queue.isEmpty() && !found){
                int size = queue.size();
                for(int i = 0;i < size;i++){
                    char[] word = queue.poll().toCharArray();
                    for(int j = 0;j < word.length;j++){
                        for(char c = 'a';c <= 'z';c++){
                            if(word[j] == c) continue;
                            char temp = word[j];
                            word[j] = c;
                            String next = new String(word);
                            if(set.contains(next)){
                                if(next.equals(endWord)) found = true;
                                map.put(next,Math.min(map.get(next),level));
                                queue.offer(next);
                                set.remove(next);
                            }
                            word[j] = temp;
                        }        
                    }
                }
                level++;
            }
            if(!found) return result;
            List<String> current = new LinkedList<>();
            current.add(beginWord);
            DFS(result,wordList,endWord,level,current,map);
            return result;
        }
        public void DFS(List<List<String>> result,Set<String> wordList,String endWord,int level,List<String> current,Map<String,Integer> map){
            if(level == current.size()){
                if(current.get(current.size() - 1).equals(endWord)) 
                    result.add(new ArrayList<String>(current));
                return;
            }
            char[] word = current.get(current.size() - 1).toCharArray();
            for(int i = 0;i < word.length;i++){
                for(char c = 'a';c <= 'z';c++){
                    if(c == word[i]) continue;
                    char temp = word[i];
                    word[i] = c;
                    String next = new String(word);
                    if(wordList.contains(next)){
                        wordList.remove(next);
                        current.add(next);
                        DFS(result,wordList,endWord,level,current,map);
                        current.remove(current.size() - 1);
                        wordList.add(next);
                    }
                    word[i] = temp;
                }
            }
        }
    }
    

    here is the bug:
    Line 4: error: incompatible types: List<String> cannot be converted to Set<String>
    I dont see incompatible on line 4.

    • list item

Log in to reply
 

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