Word Ladder II

51 篇文章 0 订阅
15 篇文章 0 订阅

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值