126 Word Ladder II

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了126 Word Ladder II相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

126. Word Ladder II

126 Word Ladder II

126. Word Ladder II(js)

126. Word Ladder II

python 126. Word Ladder II(Slack01).py

python 126. Word Ladder II(Slack01).py