Word Ladder II

来源:互联网 发布:java total memory 编辑:程序博客网 时间:2024/06/03 13:22

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.
思路: 先进行 BFS 遍历, 把结果存在 一个HashMap 的map 里, 每个单词 和 步数。  然后用 DFS 从 end 到 start 找map  里的路径 添加到list  里面去。
易错点: 1,  BFS 中添加进队列 要检查 是否在dict 里面  和  map 中没访问过的。
2. 在DFS 中,要进入下一重 DFS 时, 要检查 map 时候包含这个, 不用检查dict了, 因为map 里的肯定是dict包含的, 还有就是 步数一定要等于 上个单词的 步数 减 1 。防止回转路径循环
public class Solution {    public List<List<String>> findLadders(String start, String end, Set<String> dict) {        List<List<String>> ret = new ArrayList<List<String>>();        if(!dict.contains(start) || !dict.contains(end))            return ret;        HashMap<String, Integer> map = new HashMap<String, Integer>();        Queue<String> queue = new LinkedList<String>();        queue.add(start);        map.put(start, 0);        //Using BFS to generate a map from start to end, and storage the steps of each word.         while(!queue.isEmpty()){            String word = queue.poll();            int step = map.get(word);            if(word.equals(end))                break;            for(int i = 0; i < word.length(); i++){                char[] arr = word.toCharArray();//--- 在这里创建, 否则容易内存超出                for(char c = 'a'; c <= 'z'; c++){                    if(c == arr[i])                        continue;                    arr[i] = c;                    String cur = new String(arr);                    if(dict.contains(cur) && !map.containsKey(cur)){//--直接把 map  也作为 是否访问过的标记了,防止回转                        map.put(cur, step + 1);                        queue.add(cur);                    }                }            }        }        queue.clear();        if(!map.containsKey(end))            return ret;        List<String> sol = new ArrayList<String>();        sol.add(end);        dfs(start, end, map, sol, ret);        return ret;    }    //Using dfs to get a solution from end to start word      private void dfs(String start, String cur, HashMap<String, Integer> map, List<String> sol, List<List<String>> ret){        if(cur.equals(start)){            List<String> solCopy = new ArrayList<String>(sol);            Collections.reverse(solCopy);            ret.add(solCopy);            return;        }        int step = map.get(cur);        for(int i = 0; i < cur.length(); i++){            char[] arr = cur.toCharArray();//--- 在这里创建, 否则容易内存超出            for(char c = 'a'; c <= 'z'; c++){                if(arr[i] == c)                    continue;                arr[i] = c;                String word = new String(arr);                if(map.containsKey(word) && map.get(word) == step - 1){//--防止 回转循环                    sol.add(word);                    dfs(start, word, map, sol, ret);                    sol.remove(sol.size() - 1);                }            }        }    }}


 

0 0
原创粉丝点击