Word Ladder II

LeetCode #126


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:

  1. Only one letter can be changed at a time
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Example:

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
Return
  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]
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.

Idea:

可以搞,但是我的code总是超时,skip不管了。

Code:

class Solution {
public:

    vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
        if(find(wordList.begin(), wordList.end(), endWord)==wordList.end()) return vector<vector<string>>{};

        unordered_map<string, int> word_diff;

        vector<vector<string>> result;

        int min_level=INT_MAX;

        tuple<string, int, vector<string> > node;

        unordered_set<string> visited;

        queue<tuple<string, int, vector<string> >> w_queue;

        w_queue.push(make_tuple(beginWord, 1, vector<string>{beginWord}));

        while(!w_queue.empty()){
            node=w_queue.front();
            w_queue.pop();

            string word=get<0>(node);
            int level=get<1>(node);
            vector<string> path_sofar = get<2>(node);

            if(level>min_level) break;

            if(distanceWord(word, endWord, word_diff)==1){
                min_level=level;
                path_sofar.push_back(endWord);
                result.push_back(path_sofar);
                continue;
            } 

            visited.insert(word); // not visied, visit

            for(auto &e: wordList){
                // if child is valid, push into queue
                if(visited.find(e)==visited.end() && distanceWord(e, word, word_diff)==1){
                    path_sofar.push_back(e);
                    w_queue.push(make_tuple(e, level+1, path_sofar));
                    path_sofar.pop_back();
                }
            }
        }
        return result;
    }

    // return the number of different characters between two strings
    int distanceWord(string a, string b, unordered_map<string, int>& word_diff){
        if(a.size()!=b.size()) return -1;

        string aa=a+b, bb=b+a;

        if(word_diff.find(aa)!=word_diff.end()) return word_diff[aa];
        if(word_diff.find(bb)!=word_diff.end()) return word_diff[bb];

        int diff=0;
        for(int i=0; i<a.size(); i++){
            if(a[i]!=b[i])
                diff++;
        }
        word_diff[aa]=diff;

        return diff;
    }

};

results matching ""

    No results matching ""