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);
            }
        }
    }                             
    

      

  • 相关阅读:
    第一个java程序 Hello,World
    安装jdk,配置环境变量
    QQ传输大文件,服务器拒绝了你发送离线文件
    在WPF显示动态GIF图片
    Windows下预览svg
    WPF,ScrollViewer的属性VerticalScrollBarVisibility、HorizontalScrollBarVisibility值的区别
    未能添加对“*.dll”的引用
    端口被占用
    windows下安装tomcat
    编译安装Linux内核 centos版本
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3825062.html
Copyright © 2011-2022 走看看