126. Word Ladder II
Hard
A transformation sequence from word beginWord
to word endWord
using a dictionary wordList
is a sequence of words beginWord -> s1 -> s2 -> ... -> sk
such that:
- Every adjacent pair of words differs by a single letter.
- Every
si
for1 <= i <= k
is inwordList
. Note thatbeginWord
does not need to be inwordList
. sk == endWord
Given two words, beginWord
and endWord
, and a dictionary wordList
, return all the shortest transformation sequences from beginWord
to endWord
, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s1, s2, ..., sk]
.
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
Explanation: There are 2 shortest transformation sequences:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
"hit" -> "hot" -> "lot" -> "log" -> "cog"
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
Constraints:
1 <= beginWord.length <= 5
endWord.length == beginWord.length
1 <= wordList.length <= 1000
wordList[i].length == beginWord.length
beginWord
,endWord
, andwordList[i]
consist of lowercase English letters.beginWord != endWord
- All the words in
wordList
are unique.
struct GNode {
GNode(std::string _val): val(_val){}
void addNeighborhood(GNode* _neighbor){
neighbors.insert(_neighbor);
}
string val;
set<GNode*> neighbors;
int level = 2000;
};
struct WordGraph{
WordGraph(){}
WordGraph(string beginWord, string endWord, vector<string>& wordList){
if(nodes.find(beginWord) == nodes.end()){
n_root = new GNode(beginWord);
n_root->level = 0;
nodes[beginWord] = new GNode(beginWord);
}
if(nodes.find(endWord) == nodes.end()){
n_dest = new GNode(endWord);
n_dest->level = 2001;
nodes[endWord] = n_dest;
}
for(string &w : wordList){
if(nodes.find(w) == nodes.end()){
nodes[w] = new GNode(w);
}
}
buildLinks();
//printGraph();
}
void buildLinks(){
std::vector<GNode*> nodes_l1;
nodes_l1.emplace_back(n_root);
std::vector<GNode*> nodes_l2;
for(auto &n : nodes_l1){
nodes.erase(n->val);
}
while (!nodes_l1.empty()){
for(size_t i=0; i < nodes_l1.size(); i++){
GNode* n_current = nodes_l1[i];
for(auto it = nodes.begin(); it != nodes.end(); it++){
if(distance1(n_current->val, it->second->val)){
if(it->second->level > n_current->level)
{
n_current->addNeighborhood(it->second);
it->second->level = n_current->level + 1;
nodes_l2.emplace_back(it->second);
}
}
}
}
for(auto &n : nodes_l1){
nodes.erase(n->val);
}
nodes_l1.clear();
nodes_l1.swap(nodes_l2);
}
}
bool distance1(string &w1, string &w2){
string w1_sub = w1.substr(1, w1.size() - 1);
string w2_sub = w2.substr(1, w2.size() - 1);
if(w1_sub == w2_sub){
return true;
}
w1_sub = w1.substr(0, w1.size() - 1);
w2_sub = w2.substr(0, w2.size() - 1);
if(w1_sub == w2_sub){
return true;
}
for(int i = 1; i< w1.size() - 1; i++){
w1_sub = w1.substr(0,i) + w1.substr(i+1,w1.size()-i-1);
w2_sub = w2.substr(0,i) + w2.substr(i+1, w2.size()-i-1);
if(w1_sub == w2_sub){
return true;
}
}
return false;
}
void printGraph(){
std::cout<<n_root->val<<" "<<n_root->level<<std::endl;
set<GNode*> neighbors = n_root->neighbors;
set<GNode*> neighbors_downlevel;
while(!neighbors.empty()){
for(auto &n : neighbors){
for(auto &nei : n->neighbors){
neighbors_downlevel.insert(nei);
}
std::cout<<"val:"<<n->val<<" "<<n->level<<" ";
}
std::cout<<endl;
neighbors.clear();
neighbors.swap(neighbors_downlevel);
}
}
unordered_map<string, GNode*> nodes;
GNode* n_root;
GNode* n_dest;
};
class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
bool flag = false;
for(auto &n: wordList){
if(n == endWord){
flag = true;
break;
}
}
if(!flag)
return vector<vector<string>>();
wg_ = WordGraph(beginWord, endWord, wordList);
vector<string> path;
backtrace(wg_.n_root, wg_.n_dest, path);
if(!paths_.empty())
return paths_.begin()->second;
else
return vector<vector<string>>();
}
void backtrace(GNode* _n_cur, GNode* _n_dest, vector<string> &_path){
_path.emplace_back(_n_cur->val);
if(_n_cur->val == _n_dest->val){
paths_[_path.size()].emplace_back(_path);
return;
}
set<GNode*> neighbors_downlevel = _n_cur->neighbors;
for(auto &n: neighbors_downlevel){
backtrace(n, _n_dest, _path);
_path.pop_back();
}
//return false;
}
private:
WordGraph wg_;
map<size_t,vector<vector<string>>> paths_;
};