码迷,mamicode.com
首页 > 其他好文 > 详细

126 Word Ladder II

时间:2017-12-02 11:22:29      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:seq   ack   back   return   hat   amp   set   link   and   

Given two words (beginWord and endWord), and a dictionarys 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.

linkedin : word ladder 1.5 如果被challenge用26个char iteration太慢的话,大家怎么optimize?

在word ladder 基础上 就用了parent hash map backtracking 可以吗? 

public class WordLadder15 {
        
        Map<String, Set<String>> map = new HashMap<>();
        Map<String, String> pmap = new HashMap<>(); //parents
        
        public List<String> ladderLength(String bw, String ew, List<String> words) {
                List<String> list = new ArrayList<>(words);
                if (bw.equals(ew))
                        return null; 
                list.add(bw);
                list.add(ew);
                // build graph. From 1point 3acres bbs
                for (String w : list) {
                        Set<String> set = new HashSet<>();
                        for (String w2 : list) {
                                if (!w2.equals(w) && connect(w2, w))
                                        set.add(w2);
                        }
                        map.put(w, set);
                }
                Queue<String> q = new ArrayDeque<>();
                Set<String> seen = new HashSet<>();
                q.offer(bw);
                seen.add(bw);. From 1point 3acres bbs
                while (!q.isEmpty()) {
                        int len = q.size();
                        for (int i = 0; i < len; i++) {
                                String cur = q.poll();
                                Set<String> set = map.get(cur);
                                for (String nei : set) {
                                        if (seen.add(nei)){
                                                pmap.put(nei, cur);
                                                q.offer(nei);
                                        }
                                        if (nei.equals(ew))
                                                return getPath(nei);
                                }
                        }
                }
                return new ArrayList<String>();
        }
        
        private List<String> getPath(String end){
                List<String> res = new ArrayList<>();
                String parent=end;
                while(parent!=null){
                        res.add(0, parent);
                        parent=pmap.get(parent);
                }. From 1point 3acres bbs
                return res;
        }

        // build the graph: which connects with which
        private boolean connect(String a, String b) {
                int diff = 0;
                for (int i = 0; i < a.length(); i++) {
                        if (a.charAt(i) != b.charAt(i))
                                diff++;
                }
                return diff == 1;
        }

        public static void main(String[] args) {
                WordLadder15 sol = new WordLadder15();
                List<String> res= sol.ladderLength("hit", "cog",
                                new ArrayList<String>(Arrays.asList("hot", "dot", "dog", "lot", "log", "cog")));
                for(String s: res) System.out.println(" "+ s);
        }

}

  

126 Word Ladder II

标签:seq   ack   back   return   hat   amp   set   link   and   

原文地址:http://www.cnblogs.com/apanda009/p/7947633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!