126. 单词接龙 II

按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:

每对相邻的单词之间仅有单个字母不同。
转换过程中的每个单词 si(1 <= i <= k)必须是字典 wordList 中的单词。注意,beginWord 不必是字典 wordList 中的单词。
sk == endWord
给你两个单词 beginWord 和 endWord ,以及一个字典 wordList 。请你找出并返回所有从 beginWord 到 endWord 的 最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表 [beginWord, s1, s2, ..., sk] 的形式返回。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-ladder-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

import java.util.*;

class Solution {

    private int distance(String word1, String word2) {
        int ret = 0;

        for (int i = 0; i < word1.length(); ++i) {
            if (word1.charAt(i) != word2.charAt(i)) {
                ret++;
            }
        }

        return ret;
    }

    private Map<String, List<String>> buildGraph(List<String> wordList) {
        Map<String, List<String>> graph = new HashMap<>();

        for (int i = 0; i < wordList.size(); ++i) {
            for (int j = i + 1; j < wordList.size(); ++j) {
                if (distance(wordList.get(i), wordList.get(j)) == 1) {
                    graph.computeIfAbsent(wordList.get(i), k -> new ArrayList<>()).add(wordList.get(j));
                    graph.computeIfAbsent(wordList.get(j), k -> new ArrayList<>()).add(wordList.get(i));
                }
            }
        }

        return graph;
    }

    private Map<String, Integer> getRootDistance(String begin, Map<String, List<String>> graph) {
        Map<String, Integer> distanceMap = new HashMap<>();
        LinkedList<String> queue = new LinkedList<>();
        queue.offer(begin);
        distanceMap.put(begin, 0);

        while (!queue.isEmpty()) {
            String from = queue.poll();

            int distance = distanceMap.get(from);

            List<String> tos = graph.getOrDefault(from, Collections.emptyList());
            for (String to : tos) {
                if (!distanceMap.containsKey(to)) {
                    distanceMap.put(to, distance + 1);
                    queue.offer(to);
                }
            }
        }
        return distanceMap;
    }


    private void solve(List<List<String>> ret, LinkedList<String> path, String from, String end, Map<String, List<String>> graph,
                       Map<String, Integer> distanceMap) {
        if (from.equals(end)) {
            ret.add(new ArrayList<>(path));
            return;
        }

        List<String> tos = graph.getOrDefault(from, Collections.emptyList());

        int distance = distanceMap.get(from);

        for (String to : tos) {
            if (distance + 1 == distanceMap.get(to)) {
                path.offerLast(to);
                solve(ret, path, to, end, graph, distanceMap);
                path.pollLast();
            }
        }
    }

    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        if (wordList == null || wordList.size() == 0) {
            return Collections.emptyList();
        }

        Set<String> wordSet = new HashSet<>(wordList);

        if (!wordSet.contains(endWord)) {
            return Collections.emptyList();
        }

        wordSet.add(beginWord);

        wordList = new ArrayList<>(wordSet);

        List<List<String>> ret = new ArrayList<>();

        Map<String, List<String>> graph = buildGraph(wordList);

        Map<String, Integer> distance = getRootDistance(beginWord, graph);

        LinkedList<String> path = new LinkedList<>();

        path.offerLast(beginWord);

        solve(ret, path, beginWord, endWord, graph, distance);

        return ret;
    }
}
posted @ 2021-12-07 16:47  Tianyiya  阅读(35)  评论(0)    收藏  举报