21. Word Ladder II

2018-01-26  本文已影响0人  邓博文_7c0a

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<string> &dictionary,
                      unordered_map<string, unordered_set<string> > &prev, unordered_set<string> &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<vector<string> > &paths, string curr, string beginWord,
                     unordered_map<string, unordered_set<string> > &prev) {
        if (curr == beginWord) {
            paths.push_back(vector<string> {curr});
        } else if (prev.find(curr) != prev.end()) {
            for (string previous_word : prev[curr]) {
                vector<vector<string> > 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<vector<string> > findLadders(string beginWord, string endWord, vector<string> &wordList) {
        unordered_set<string> dictionary;
        for (auto it = wordList.begin(); it != wordList.end(); ++it) dictionary.insert(*it);
        unordered_map<string, unordered_set<string> > prev;
        prev[beginWord].insert("");
        vector<vector<string> > rtn;
        unordered_set<string> level = {beginWord};
        while (!level.empty()) {
            unordered_set<string> new_level;
            unordered_map<string, unordered_set<string> > new_prev;
            for (string curr : level) {
                if (curr == endWord) {
                    // Summarize the output
                    collectPaths(rtn, curr, beginWord, prev);
                    return rtn;
                }
                unordered_set<string> 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

上一篇 下一篇

猜你喜欢

热点阅读