*** 126. Word Ladder II

Borris發表於2019-10-23

解法一

思路

一個思路是通過 DFS 解決這道題。從 beginWord 開始,在字典中找出所有可能與之相鄰的單詞,即與之差一個字母的單詞。對於所有的這些單詞,再進行 DFS。遞迴出口是:如果查到 endWord, 將路徑新增到結果中。如果查詢到的路徑比在結果集中的路徑短,把之前的結果清除,新增新的最短路徑。如果當前查詢路徑比已經查詢到的最短路徑長,終止查詢。
獲取一個 word 所有與之相鄰的 words,可以將這個字母每一位分別用26個字母代替,和 wordList 比較。程式碼如下:

public ArrayList<String> findNeighbors(String curr, Set<String> wordSet) {
    char[] chars = curr.toCharArray();
    ArrayList<String> neighbors = new ArrayList<>();
    for (int i = 0; i < chars.length; i++) {
        char old = chars[i];
        for (char j = 'a', j <= 'z'; j++) {
            if (j == old) continue;
            chars[i] = j;
            String newStr = new String(chars);
            if (set.contains(newStr)) {
                neighbors.add(newStr);
            }
        }
        chars[i] = old; // Restore the word
    }
    return neighbors;
}

遞迴出口如下:

// ans list has the resulting paths
// min is the length of the shortest path
// temp is the current path
if (beginWord.equals(endWord)) {
    if (temp.size < min) {
        ans.clear();
        ans.add(new ArrayList<String>(temp));
        min =  temp.size();
    } else if (temp.size() == min) {
        ans.add(new ArrayList<String>(temp));
    }
}

if (temp.size() > min) {
    return;
}

隨後我們可以用遞迴實現 DFS 來得到最短路徑。

程式碼
Class Solution{
    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        List<List<String>> ans = new ArrayList<>();
        ArrayList<String> temp = new ArrayList<>();
        Set<String> dict = new HashSet<>(wordList);

        temp.add(beginWord);
        findLaddersHelper(beginWord, endWord, dict, ans, temp);
        return ans;

    }

    private int min = Integer.MAX_VALUE;
    public void findLaddersHelper(String beginWord, String endWord, Set<String> dict, List<List<String>> ans, ArrayList<String> temp) {
        if (beginWord.equals(endWord)) {
            if (temp.size() < min) {
                ans.clear();
                ans.add(new ArrayList<String>(temp));
                min = temp.size();
            } else {
                ans.add(new ArrayList<String>(temp));
            }
        }

        if (temp.size() > min) {
            return;
        }

        List<String> neighbors = findNeighbors(beginWord, dict);
        for (String neighbor : neighbors) {
            if (temp.contains(neighbor)) // if neighbor is already in temp, skip to next neighbor
                continue;
            temp.add(neighbor);
            findLaddersHelper(neighbor, endWord, dict, ans, temp);
            temp.remove(temp.size() - 1); // ?????
        }
    }

    public ArrayList<String> findNeighbors(String curr, Set<String> dict) {
        ArrayList<String> neighbors = new ArrayList<>();
        char[] chars = curr.toCharArray();

        // Substitute each character with a - z
        for (int i = 0; i < chars.length; i++) {
            char old = chars[i];
            for (char c = 'a'; c <= 'z'; c++) {
                if (c == old) {
                    continue;
                }
                chars[i] = c;
                String newWord = new String(chars);
                if (dict.contains(newWord)) {
                    neighbors.add(newWord);
                }
            }
            chars[i] = old;
        }
        return neighbors;
    }
}
複雜度分析
  • 時間複雜度
    這個演算法含有過多不必要的重複遍歷,說以會超時。
  • 空間複雜度

解法二

思路
程式碼
複雜度分析
  • 時間複雜度
    • 最好情況
    • 最壞情況
    • 平均情況
  • 空間複雜度

Takeaway