leetcode 126. Word Ladder II
2017-09-13 15:49
393 查看
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.
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.
题目比较难,硬来会TLE。
为了节约时间,解法分为两步:
1、做一次BFS,给list里面每一个单词都做一次标记,标记和end之间的距离。
2、这样在之后的DFS的时候就能只向着距离变短的方向找。
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.
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.
题目比较难,硬来会TLE。
为了节约时间,解法分为两步:
1、做一次BFS,给list里面每一个单词都做一次标记,标记和end之间的距离。
2、这样在之后的DFS的时候就能只向着距离变短的方向找。
class Solution { public: vector<vector<string>> findLadders(string start, string end, vector<string>& wList) { unordered_set<string> dict; //unordered_set不需要额外去重 for(int i = 0; i < wList.size(); i++) dict.insert(wList[i]); vector<vector<string>> ret; if (dict.find(end) == dict.end()) return ret; else dict.erase(end); unordered_set<string> dic = dict; dic.insert(start); //BFS map<string, int> mmap; //存每个string到end的距离 queue<string> que; //bfs标配,从end开始扩散 que.push(end); int far = 0; //far存到end的距离 while(!que.empty()) { int size = que.size(); for(int i = 0; i < size; i++) { string temp = que.front(); que.pop(); mmap[temp] = far; for(int j = 0; j < temp.size(); j++) { char oldchar = temp[j]; for(char a = 'a'; a <= 'z'; a++) { if(a == oldchar) continue; temp[j] = a; if (dic.find(temp) != dic.end()) { que.push(temp); dic.erase(temp); } } temp[j] = oldchar; } } far++; } for(auto it = mmap.begin(); it != mmap.end() ;it++) cout<<it->first<<" "<<it->second<<endl; //DFS 只能用递归的这种方式 vector<string> m1(1, start); //m1里面存的是过程中的解 helper(ret, m1, dict, end, mmap); return ret; } void helper(vector<vector<string>> & ret, vector<string> m1, unordered_set<string> &dict, string end, map<string, int> &mmap) { string start = m1[m1.size() - 1]; int old_distance = mmap[start]; for(int i = 0; i < start.size(); i++) { char oldchar = start[i]; for(char a = 'a'; a <= 'z'; a++) { start[i] = a; if(start == end) { m1.push_back(end); ret.push_back(m1); return; } if(dict.find(start) != dict.end() && mmap[start] < old_distance) { m1.push_back(start); helper(ret, m1, dict, end, mmap); m1.pop_back(); } } start[i] = oldchar; } } };
相关文章推荐
- 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 bfs dfs
- Leetcode 127. Word Ladder I & 126. Word Ladder II
- Leetcode(W5):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
- leetcode 126 —— Word Ladder II
- LeetCode126—Word Ladder II
- leetcode 126. Word Ladder II
- [leetcode 126] Word Ladder II
- Leetcode 126. Word Ladder II[hard]
- 【LeetCode】126. Word Ladder II【H】
- 【leetcode】String——Word Ladder II(126)
- [leetcode] 126.Word Ladder II