Hard-题目54:126. Word Ladder II

题目原文:
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 intermediate word must exist in the word list
For example,
Given:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”]
Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

题目大意:
给出两个单词,及一个词典,输出所有从单词start到单词end的转换序列,要求:
(1) 每次只能转换一个字母;
(2) 每个中间单词都必须出现在字典中。
题目分析:
这道题传说是leetcode的最难一题,对时间和空间的要求都非常严格。我只能理解朴素解法,即把start,end,和词典中每个单词都看做顶点,相邻单词(只差一个字母的单词)都看做边,然后从start开始跑一遍dijkstra即可。
但这样做会超时,中间又有很多优化的过程。限于水平真的无法理解。
源码:(language:java)

public class Solution {
    public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) {
        HashMap<String, ArrayList<String>> h = new HashMap();
        Set<String> set1 = new HashSet(), set2 = new HashSet();
        set1.add(beginWord); set2.add(endWord);
        BFS(set1, set2, wordList, h, true);

        List<List<String>> ans = new ArrayList();
        List<String> cur = new ArrayList();
        cur.add(beginWord);
        DFS(beginWord, endWord, h, cur, ans);
        return ans;
    }

    private void BFS(Set<String> set1, Set<String> set2, Set<String> wordList, HashMap<String, ArrayList<String>> h, boolean forward) {
        if (set1.size() > set2.size()) {
            BFS(set2, set1, wordList, h, !forward);
            return;
        }
        wordList.removeAll(set1);
        wordList.removeAll(set2);
        boolean connected = false;
        Set<String> set3 = new HashSet();

        for (String s : set1) {
            char[] c = s.toCharArray();
            for (int i = 0, len = c.length; i < len; i++) {
                char ch = c[i];
                for (char x = 'a'; x <= 'z'; x++)
                    if (x != ch) {
                        c[i] = x;
                        String cand = new String(c);
                        if (set2.contains(cand) || (!connected && wordList.contains(cand))) {
                            if (set2.contains(cand))
                                connected = true;
                            else
                                set3.add(cand);

                            String cand1 = forward ? cand : s;
                            String s1 = forward ? s : cand;
                            ArrayList<String> cur = h.containsKey(s1) ? h.get(s1) : new ArrayList();
                            cur.add(cand1);
                            h.put(s1, cur);
                        }
                    }
                c[i] = ch;
            }
        }
        if (!connected && !set3.isEmpty())
            BFS(set3, set2, wordList, h, forward);
        }

        private void DFS(String str, String ed, HashMap<String, ArrayList<String>> h, List<String> cur, List<List<String>> ans) {
            if (str.equals(ed)) {
                ans.add(new ArrayList(cur));
                return;
            }

            if (!h.containsKey(str)) return;
            List<String> next = h.get(str);
            for (String i : next) {
                cur.add(i);
                DFS(i, ed, h, cur, ans);
                cur.remove(cur.size() - 1);
            }
        }
}

成绩:
26ms,99.87%,147ms,1.59%

你可能感兴趣的:(Hard-题目54:126. Word Ladder II)