zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }                             
    

      

  • 相关阅读:
    linux 文件时间
    linux命令学习 touch
    linux命令学习 cp
    Linux命令学习 mv
    SpringMVC mvc详解
    JDBC原生态代码
    java中静态方法中调用非静态方法的详解
    Spring技术详解
    Spring中的 BeanFactory和 ApplicationContext的区别与解释
    java多线程编程相关技术
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3825062.html
Copyright © 2011-2022 走看看