Leetcode:Word Ladder II

来源:互联网 发布:万达怎么了 知乎 编辑:程序博客网 时间:2024/05/29 08:04

Url

https://leetcode.com/submissions/detail/129718140/

描述

Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

Only one letter can be changed at a timeEach transformed word must exist in the word list. Note that beginWord is not a transformed word.

For example,

Given:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”,”cog”]

Return

[
[“hit”,”hot”,”dot”,”dog”,”cog”],
[“hit”,”hot”,”lot”,”log”,”cog”]
]

Note:

Return an empty list if there is no such transformation sequence.All words have the same length.All words contain only lowercase alphabetic characters.You may assume no duplicates in the word list.You may assume beginWord and endWord are non-empty and are not the same.

UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

思路

  1. 首先BFS计算beginword到每一个word的距离;
  2. 再DFS 计算到endword的路径

代码

private List<List<String>> res = new ArrayList<>();    private List<String> getNeighbours(String cur,Set<String> wordSet){        List<String> neighbour = new ArrayList<>();        char old;        char[] curChar = cur.toCharArray();        for(char i='a';i<='z';i++){            for(int j=0;j<cur.length();j++){                old = curChar[j];                if(i==old) continue;                curChar[j] = i;                String ns = new String(curChar);                if(wordSet.contains(ns)){                    neighbour.add(ns);                }                curChar[j] = old;            }        }        return neighbour;    }    private Map<String,Integer> getDistanceFromBegin(String beginWord,Map<String,List<String>> neightbourMap){        Map<String,Integer> distance = new HashMap<String,Integer>();        distance.put(beginWord,0);        Queue<String> queue = new LinkedList<>();        queue.add(beginWord);        while(!queue.isEmpty()){            String cur = queue.poll();            List<String> neightbours = neightbourMap.get(cur);            for(String neightbour:neightbours){                if(!distance.containsKey(neightbour)){                    queue.add(neightbour);                    distance.put(neightbour,distance.get(cur)+1);                }            }        }        return distance;    }    private void dfs(List<String> path,String target,Map<String,List<String>>neightbourMap,Map<String,Integer> distance){        String last = path.get(path.size()-1);        if(last.equals(target)){            res.add(new ArrayList(path));            return;        }        List<String> neightbours = neightbourMap.get(last);        for(String neightbour:neightbours){            if(distance.get(neightbour)==distance.get(last)+1){                path.add(neightbour);                dfs(path,target,neightbourMap,distance);                path.remove(path.size()-1);            }        }    }    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {        Set<String> wordSet = new HashSet<String>(wordList);        Map<String,List<String>> neightbourMap = new HashMap<String,List<String>>();        neightbourMap.put(beginWord,getNeighbours(beginWord,wordSet));        for(String word:wordList){            neightbourMap.put(word,getNeighbours(word,wordSet));        }        Map<String,Integer> distance = getDistanceFromBegin(beginWord,neightbourMap);        List<String> path = new LinkedList<String>();        path.add(beginWord);        dfs(path,endWord,neightbourMap,distance);        return res;    }