1. 程式人生 > 程式設計 >JavaScript兩張圖搞懂原型鏈

JavaScript兩張圖搞懂原型鏈

按字典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;
    }
}
心之所向,素履以往 生如逆旅,一葦以航