21. Word Ladder II

Link to the problem

Description

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 transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:
Return an empty list if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.

Example

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

Idea

Variant of BFS, need to handle ties properly.

Solution

class Solution {
private:
    void getNeighbors(string curr, unordered_set &dictionary,
                      unordered_map > &prev, unordered_set &neighbors) {
        int n = curr.size();
        for (int i = 0; i < n; i++) {
            char orig = curr[i];
            for (int j = 0; j < 26; j++) {
                curr[i] = 'a' + j;
                if (dictionary.find(curr) != dictionary.end() &&
                   prev.find(curr) == prev.end()) {
                    neighbors.insert(curr);
                }
            }
            curr[i] = orig;
        }
    }
    
    void collectPaths(vector > &paths, string curr, string beginWord,
                     unordered_map > &prev) {
        if (curr == beginWord) {
            paths.push_back(vector {curr});
        } else if (prev.find(curr) != prev.end()) {
            for (string previous_word : prev[curr]) {
                vector > previous_paths;
                collectPaths(previous_paths, previous_word, beginWord, prev);
                for (auto &previous_path : previous_paths) {
                    previous_path.push_back(curr);
                    paths.push_back(previous_path);
                }
            }
        }
    }

public:
    vector > findLadders(string beginWord, string endWord, vector &wordList) {
        unordered_set dictionary;
        for (auto it = wordList.begin(); it != wordList.end(); ++it) dictionary.insert(*it);
        unordered_map > prev;
        prev[beginWord].insert("");
        vector > rtn;
        unordered_set level = {beginWord};
        while (!level.empty()) {
            unordered_set new_level;
            unordered_map > new_prev;
            for (string curr : level) {
                if (curr == endWord) {
                    // Summarize the output
                    collectPaths(rtn, curr, beginWord, prev);
                    return rtn;
                }
                unordered_set neighbors;
                getNeighbors(curr, dictionary, prev, neighbors);
                for (string nbr : neighbors) {
                    new_level.insert(nbr);
                    new_prev[nbr].insert(curr);
                }
            }
            for (auto it = new_prev.begin(); it != new_prev.end(); ++it) {
                for (auto &parent : it->second) {
                    prev[it->first].insert(parent);
                }
            }
            level = new_level;
        }
        return rtn;
    }
};

39 / 39 test cases passed.
Runtime: 174 ms

你可能感兴趣的:(21. Word Ladder II)