[leetcode 126] Word Ladder II
2014-12-28 15:30
399 查看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start =
end =
dict =
Return
Note:
All words have the same length.
All words contain only lowercase alphabetic characters.
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start =
"hit"
end =
"cog"
dict =
["hot","dot","dog","lot","log"]
Return
[ ["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ]
Note:
All words have the same length.
All words contain only lowercase alphabetic characters.
class Solution { public: vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { result_.clear(); unordered_map<string, vector<string>> prevMap; for(auto iter = dict.begin(); iter != dict.end(); ++iter) prevMap[*iter] = vector<string>(); vector<unordered_set<string>> candidates(2); int current = 0; int previous = 1; candidates[current].insert(start); while(true) { current = !current; previous = !previous; for (auto iter = candidates[previous].begin(); iter != candidates[previous].end(); ++iter) dict.erase(*iter); candidates[current].clear(); for(auto iter = candidates[previous].begin(); iter != candidates[previous].end(); ++iter) { for(size_t pos = 0; pos < iter->size(); ++pos) { string word = *iter; for(int i = 'a'; i <= 'z'; ++i) { if(word[pos] == i)continue; word[pos] = i; if(dict.count(word) > 0) { prevMap[word].push_back(*iter); candidates[current].insert(word); } } } } if (candidates[current].size() == 0) return result_; if (candidates[current].count(end)) break; } vector<string> path; GeneratePath(prevMap, path, end); return result_; } private: void GeneratePath(unordered_map<string, vector<string>> &prevMap, vector<string>& path, const string& word) { if (prevMap[word].size() == 0) { path.push_back(word); vector<string> curPath = path; reverse(curPath.begin(), curPath.end()); result_.push_back(curPath); path.pop_back(); return; } path.push_back(word); for (auto iter = prevMap[word].begin(); iter != prevMap[word].end(); ++iter) GeneratePath(prevMap, path, *iter); path.pop_back(); } vector<vector<string>> result_; };
相关文章推荐
- Leetcode 126. Word Ladder II
- [LeetCode] 126. Word Ladder II 词语阶梯 II
- leetcode[126]Word Ladder II
- leetcode_c++:Word Ladder II(126)
- [LeetCode]题解(python):126-Word Ladder II
- [LeetCode] 126. Word Ladder II
- Leetcode 126. Word Ladder II
- LeetCode[126.Word Ladder II]题解 难度[hard]
- Leetcode 126. Word Ladder II 字符变换2 解题报告
- [leetcode]126. Word LadderII@Java解题报告
- LeetCode力扣之126. Word Ladder II
- Leetcode 126 word ladder II
- [Leetcode] 126. Word Ladder II 解题报告
- leetcode126 Word Ladder II
- [算法分析与设计] leetcode 每周一题: 126. Word Ladder II
- LeetCode 126. Word Ladder II
- Leetcode 126. Word Ladder II
- [leetcode] 126. Word Ladder II 解题报告
- [LeetCode]126 Word Ladder II
- [Leetcode 126] Word Ladder II