126 Word Ladder II

126
Word Ladder II
13.2%
Hard
所有的words可以组成一张图。节点是word,如果有一个字母不一样,那么这两个节点之间有一条边。
可以从start -> end (找到后的添加顺序是end->start),也可以end->start(添加顺序相反,start->end)
hashmap存查找的路径
end-> start

public class Solution {
    public List> findLadders(String start, String end, Set wordList) {
        //search from end to start
        List> rst = new ArrayList>();
        wordList.remove(end);
        wordList.add(start);
        HashMap> path = new HashMap>();
        path.put(end, null);
        HashMap> pathHasAdded = new HashMap>();
        pathHasAdded.put(end, null);
        while (!path.containsKey(start)){
            HashMap> pathToBeAdded = new HashMap>();
            for (String word:pathHasAdded.keySet()){
                ArrayList neighbourWords = neighbourWords(word);
                for (String nword:neighbourWords){
                    if (!path.containsKey(nword) && wordList.contains(nword)){
                        if (!pathToBeAdded.containsKey(nword)) pathToBeAdded.put(nword, new ArrayList());
                        pathToBeAdded.get(nword).add(word);
                    }
                }
            }
            if (pathToBeAdded.isEmpty()) return rst;
            path.putAll(pathToBeAdded);
            pathHasAdded = pathToBeAdded;
        }
        LinkedList output = new LinkedList();
        output.add(start);
        dfs(rst, path, output,end);
        return rst;
    }
    private ArrayList neighbourWords(String word){
        ArrayList rst = new ArrayList();
        char[] charArray = word.toCharArray();
        for (int i=0; i> rst, HashMap> map, LinkedList output, String end){
        if (output.getLast().equals(end)){
            rst.add((List)output.clone());
            return;
        }
        for (String nextWord:map.get(output.getLast())){
            output.addLast(nextWord);
            dfs(rst, map, output, end);
            output.removeLast();
        }
    }
}

start->end

public class Solution {
    public List> findLadders(String start, String end, Set dict) {
        dict.add(end);
        dict.remove(start);
        HashMap> map = new HashMap>();
        // (a,b,c) -> d
        HashSet currSearch = new HashSet();
        currSearch.add(start);
        while (!currSearch.isEmpty()){
            HashSet nextSearch = new HashSet();
            for (String s:currSearch){
                ArrayList sList = transform(s);
                for (String sNext:sList){
                    if (dict.contains(sNext)){
                        if (!map.containsKey(sNext)) map.put(sNext,new ArrayList());
                        map.get(sNext).add(s);
                        nextSearch.add(sNext);
                    }
                }
            }
            if (nextSearch.contains(end)) break;
            dict.removeAll(nextSearch);
            currSearch = nextSearch;
        }
        List> rst = new ArrayList>();
        if (!map.containsKey(end)) return rst;
        LinkedList output = new LinkedList();
        output.addFirst(end);
        dfs(rst,map,output,start);
        return rst;
    }
    private ArrayList transform(String s){
        char[] cha = s.toCharArray();
        ArrayList list = new ArrayList();
        for (int i=0; i> rst, HashMap> map, LinkedList output, String start){
        if (output.getFirst().equals(start)){
            rst.add((List)output.clone());
            return;
        }
        for (String prevString:map.get(output.getFirst())){
            output.addFirst(prevString);
            dfs(rst,map,output,start);
            output.removeFirst();
        }
    }
}

你可能感兴趣的:(126 Word Ladder II)