Hard-题目54:126. Word Ladder II

转载 2016年05月31日 23:59:35

题目原文:
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%

相关文章推荐

[Leetcode 126 & 127, hard & medium] Word Ladder II & I

[Leetcode 126 & 127, hard & medium] Word Ladder II & I

LeetCode[126.Word Ladder II]题解 难度[hard]

**题目** Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest tra...

Leetcode 126. Word Ladder II[hard]

题目: Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transf...

126. Word Ladder I & II

一、题目简述给定两个单词(beginWord和endWord),和一个单词字典,找到所有从beginWord到endWord的最短转换,使得: 每次只能改变一个字母; 每个转换单词都必须在单词字典中,...

[LeetCode] 126. Word Ladder II java

采用BFS广度搜索 先bfs找到endword,同时存储每个单词所处层数 再以endword为目标,往回找到startword,生成路径。 HashMap path = new HashMap()...

【LeetCode】126. Word Ladder II【H】

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat...

[leetcode] 126. Word Ladder II 解题报告

题目链接: https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a...

LeetCode126 Word Ladder II

详细见:leetcode.com/problems/word-ladder-ii Java Solution: github package leetcode; import jav...

[leetcode 126] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from...

第四周:[leetcode] 126. Word Ladder II

126. Word Ladder II
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)