Leetcode:NO.126 单词接龙 II 广度优先搜索

题目

给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:

每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:

如果不存在这样的转换序列,返回一个空列表。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。

示例 1:

输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

输出:
[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]
示例 2:

输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

输出: []


解释: endWord "cog" 不在字典中,所以不存在符合要求的转换序列。

链接:https://leetcode-cn.com/problems/word-ladder-ii

解题记录

  • 通过和beginword的比对获得,第一层字符串,就是跟beginword只相差一个字符的那些字符
  • 使用队列存储每一层放入的数组
  • 再次跟第一层的每一个字符串进行比对,于第一层只差一个字符的字符串加入列表作为第二层,新的数组存入队列
  • 以此类推,直到出现跟endword相同字符串出现为止
  • Set distSet = new HashSet<>(wordList);这个很重要不换成set的话会超时
    Leetcode:NO.126 单词接龙 II 广度优先搜索_第1张图片
/**
 * @author ffzs
 * @describe
 * @date 2020/6/7
 */
public class Solution {
    public static List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        List<List<String>> res = new ArrayList<>();
        Set<String> distSet = new HashSet<>(wordList);
        if (!distSet.contains(endWord)) return res;
        Set<String> visited = new HashSet<>();
        visited.add(beginWord);
        Queue<String[]> queue = new LinkedList<>();
        String[] ls = {beginWord};
        queue.add(ls);
        boolean flag = false;
        int level = 0;
        while (!queue.isEmpty() && !flag) {
            int size = queue.size();
            Set<String> subVisited = new HashSet<>();
            for (int i = 0; i < size; i++) {
                String[] route = queue.poll();
                assert route != null;
                String word = route[level];
                char[] chars = word.toCharArray();
                for (int j = 0; j < word.length(); j++) {
                    char tmp = chars[j];
                    for (char ch = 'a'; ch <= 'z'; ch ++) {
                        chars[j] = ch;
                        if (tmp == ch) continue;
                        String str = new String(chars);
                        if (distSet.contains(str) && !visited.contains(str)) {
                            String[] routeNext = Arrays.copyOf(route, level+2);
                            routeNext[level+1] = str;
                            if (str.equals(endWord)) {
                                flag = true;
                                res.add(List.of(routeNext));
                            }
                            else subVisited.add(str);
                            queue.add(routeNext);
                        }
                    }
                    chars[j] = tmp;
                }
            }
            visited.addAll(subVisited);
            level++;
        }
        return res;
    }

    public static void main(String[] args) {
        String begin="red", end = "tax";
        String[] strs = {"ted","tex","red","tax","tad","den","rex","pee"};
        List<String> list = List.of(strs);
        System.out.println(findLadders(begin, end, list));
    }
}

在这里插入图片描述
先这样有时间再优化一下

  • 双向BFS + DFS
  • 构建桶

你可能感兴趣的:(算法)