leetcode--Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

 

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

Not found the correct solution yet.

 

public class Solution {
    public List<List<String>> findLadders(String start, String end, Set<String> dict) {
                                           List<List<String>> soln = new ArrayList<List<String>>();
        int len = start.length();
                
        boolean exist = false; 
        Set<String> chk = new HashSet<String>();
        Map<String, List<String>> graph = new HashMap<String, List<String>>();
        Queue<String> level = new LinkedList<String>();
        level.add(start); chk.add(start); dict.add(end);
        while(level.peek() != null) {
            Set<String> toBuild = new HashSet<String>();
            Queue<String> nextLevel = new LinkedList<String>();
            while(level.peek() != null) {
                List<String> neighbor = new ArrayList<String>();
                String wd = level.poll();
                char[] word = wd.toCharArray();
                for(int i = 0; i < len; ++i) {
                    char ch = word[i];
                    for(char c = 'a'; c <= 'z'; ++c){
                        word[i] = c;
                        String nword = new String(word);
                        if(dict.contains(nword) && !chk.contains(nword)) {
                            if(toBuild.add(nword))          nextLevel.add(nword);
                            neighbor.add(nword);                                                     
                        }
                        exist = exist || nword.equals(end);
                    }
                    word[i] = ch;
                }
                graph.put(wd, neighbor);
            }
            chk.addAll(toBuild);
            if(exist)       break;
            level = nextLevel;
        }
                
        if(exist)       dfs(start, end, graph, new ArrayList<String>(), soln);
        return soln;
    }
        
    private void dfs(String start, String end, Map<String, List<String>> graph, 
                     List<String> oneSoln, List<List<String>> soln){
        if(start.equals(end)){
            List<String> oneSolution = new ArrayList<String>();
            oneSolution.addAll(oneSoln);
            oneSolution.add(start);
            soln.add(oneSolution);
            return; 
        }
        if(!graph.containsKey(start))   return;
        if(graph.containsKey(start)) {
            oneSoln.add(start);
            List<String> alist = graph.get(start);
            for(int i = 0; i < alist.size(); ++i)
                dfs(alist.get(i), end, graph, oneSoln, soln);
            oneSoln.remove(oneSoln.size() - 1);
        }
    }
}                             

  

posted @ 2014-07-04 20:40  Averill Zheng  阅读(161)  评论(0编辑  收藏  举报