Leetcode:Word Ladder II

Url

https://leetcode.com/submissions/detail/129718140/

描述

Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

Only one letter can be changed at a time
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

For example,

Given:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”,”cog”]

Return

[
[“hit”,”hot”,”dot”,”dog”,”cog”],
[“hit”,”hot”,”lot”,”log”,”cog”]
]

Note:

Return an empty list if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.

UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

思路

  1. 首先BFS计算beginword到每一个word的距离;
  2. 再DFS 计算到endword的路径

代码

private List<List<String>> res = new ArrayList<>();

    private List<String> getNeighbours(String cur,Set<String> wordSet){
        List<String> neighbour = new ArrayList<>();

        char old;
        char[] curChar = cur.toCharArray();
        for(char i='a';i<='z';i++){
            for(int j=0;j<cur.length();j++){
                old = curChar[j];
                if(i==old) continue;
                curChar[j] = i;
                String ns = new String(curChar);
                if(wordSet.contains(ns)){
                    neighbour.add(ns);
                }
                curChar[j] = old;
            }
        }
        return neighbour;
    }

    private Map<String,Integer> getDistanceFromBegin(String beginWord,Map<String,List<String>> neightbourMap){
        Map<String,Integer> distance = new HashMap<String,Integer>();
        distance.put(beginWord,0);
        Queue<String> queue = new LinkedList<>();
        queue.add(beginWord);
        while(!queue.isEmpty()){
            String cur = queue.poll();
            List<String> neightbours = neightbourMap.get(cur);
            for(String neightbour:neightbours){
                if(!distance.containsKey(neightbour)){
                    queue.add(neightbour);
                    distance.put(neightbour,distance.get(cur)+1);
                }
            }
        }
        return distance;
    }


    private void dfs(List<String> path,String target,Map<String,List<String>>neightbourMap,Map<String,Integer> distance){
        String last = path.get(path.size()-1);
        if(last.equals(target)){
            res.add(new ArrayList(path));
            return;
        }
        List<String> neightbours = neightbourMap.get(last);
        for(String neightbour:neightbours){
            if(distance.get(neightbour)==distance.get(last)+1){
                path.add(neightbour);
                dfs(path,target,neightbourMap,distance);
                path.remove(path.size()-1);
            }
        }
    }

    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        Set<String> wordSet = new HashSet<String>(wordList);
        Map<String,List<String>> neightbourMap = new HashMap<String,List<String>>();
        neightbourMap.put(beginWord,getNeighbours(beginWord,wordSet));
        for(String word:wordList){
            neightbourMap.put(word,getNeighbours(word,wordSet));
        }
        Map<String,Integer> distance = getDistanceFromBegin(beginWord,neightbourMap);
        List<String> path = new LinkedList<String>();
        path.add(beginWord);
        dfs(path,endWord,neightbourMap,distance);
        return res;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值