Question :
Given two words (startandend), and a dictionary, find all shortest transformation sequence(s) fromstarttoend, such that:
- 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.
For Example:
Graph Array = [
h i t
h o t
d o t
d o g
l o t
l o g
c o g
]
Anwser 1 :
class Solution {
public:
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (dict.find(start) == dict.end()) {
dict.insert(start);
}
if (dict.find(end) == dict.end()) {
dict.insert(end);
}
queue<string> q;
unordered_map<string, pair<int, vector<string> > > dep_map;
dep_map[start] = pair<int, vector<string> >(1, vector<string>());
q.push(start);
int depth = 1;
int len = start.length(); // word length
int min_depth = dict.size() + 2; // 2 for start and end words
while (!q.empty() && min_depth > depth) {
string cur = q.front();
q.pop();
depth = dep_map[cur].first;
for (int i=0; i<len; ++i) // check word(s)
{
string adj = cur;
for (char a='a'; a<='z'; ++a) {
if (a==cur[i]) continue;
adj[i] = a;
if (adj == end) {
min_depth = depth+1;
}
if (dict.find(adj) != dict.end())
{
unordered_map<string, pair<int, vector<string> > > ::iterator it = dep_map.find(adj);
if(it == dep_map.end()) {
pair<int, vector<string> > value(depth + 1, vector<string>());
value.second.push_back(cur);
dep_map[adj] = value;
q.push(adj);
} else if ((it->second).first == depth+1) {
it->second.second.push_back(cur);
}
}
}
}
} // end while
vector<vector<string> > result;
if (min_depth == dict.size() + 2) { // only for start and end words
return result;
}
vector<string> stack;
vector<string> sequence;
stack.push_back(end);
while(!stack.empty()) {
string top = stack.back();
stack.pop_back();
sequence.push_back(top);
vector<string> &sons = dep_map[top].second;
for(int i=0; i<sons.size(); ++i) {
stack.push_back(sons[i]);
}
if (sons.size()==0) {
int index = result.size();
result.push_back(vector<string>());
for (int i=sequence.size()-1; i>=0; --i) {
result[index].push_back(sequence[i]); // save result
}
top = sequence.back();
sequence.pop_back();
while(!sequence.empty())
{
string father = sequence.back();
vector<string> brothers = dep_map[father].second;
if (top != brothers[0]) {
break;
}
sequence.pop_back();
top = father;
}
}
}
return result;
}
};
Anwser 2 :
class Solution {
public:
struct Node {
vector<string> parent;
int layer;
};
void buildResult( vector<vector<string>> &result, list<string> &r, string start, string end, unordered_map<string, Node> &checkmap)
{
r.push_front(end);
if(end==start) {
vector<string> oneresult(r.begin(), r.end());
result.push_back(oneresult);
} else {
for(const string &s:checkmap[end].parent) {
buildResult(result, r, start, s, checkmap);
}
}
r.pop_front();
}
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
unordered_map<string, Node> checkmap;
list<string> layer;
checkmap[start].layer = 1;
checkmap[start].parent.push_back(start);
layer.push_back(start);
vector<vector<string>> result;
bool hasfind = false;
string layerend = start;
string last;
while(!layer.empty())
{
string s = layer.front();
layer.pop_front();
int currentlayer = checkmap[s].layer;
int L = s.size();
for(int i = 0; i< L; i++)
{
string temp = s;
for(int j = 0; j < 26; j++)
{
temp[i] = j+'a';
if(temp==end) {
checkmap[temp].parent.push_back(s);
checkmap[temp].layer = checkmap[s].layer+1;
hasfind = true;
} else if(dict.count(temp)&&s!=temp) {
if(checkmap.count(temp)==0){
checkmap[temp].parent.push_back(s);
checkmap[temp].layer = checkmap[s].layer+1;
layer.push_back(temp);
last = temp;
} else if(checkmap[s].layer<checkmap[temp].layer) {
checkmap[temp].parent.push_back(s);
}
}
}
} // end for
if(s == layerend)
{
layerend = last;
if(hasfind) {
list<string> r;
buildResult(result, r, start, end, checkmap);
break;
}
}
} // end while
return result;
}
};
参考推荐:
Word Ladder II
stack
分享到:
相关推荐
126.Word_Ladder_II_词语阶梯二【LeetCode单题讲解系列】
python python_leetcode题解之126_Word_Ladder_II
javascript js_leetcode题解之126-word-ladder-ii.js
python python_leetcode题解之127_Word_Ladder
javascript js_leetcode题解之127-word-ladder.js
经典字梯游戏c++代码,经测试通过的哦,leetcode上面的题目
* Word Break、Word Break II:该题目要求将字符串分割成单词,实现方法使用了动态规划和回溯算法。 * Word Ladder:该题目要求将一个单词转换成另一个单词,实现方法使用了广度优先搜索算法。 二、树和图 * ...
在LeetCode上,单词接龙类问题可能表现为“Anagrams”(同字母异序词)或者“Word Ladder”(单词阶梯),这些题目要求高效地搜索满足条件的单词序列。 【压缩包子文件的文件名称列表】"LeetCode-main"可能包含了一...
1. leetCode-126-Word-LadderII.md:这涉及到第126题,词梯问题,通常是一个字符串转换问题,目标是找到两个单词之间的最短转换序列,每次只改变一个字母。 2. leetcode-224-Basic-Calculator.md:这是第224题,基础...
6. Word Ladder 单词梯是一个字符串问题,要求将一个单词转换为另一个单词,并且每次转换只能改变一个字符。可以使用广度优先搜索或深度优先搜索来解决该问题。 7. Median of Two Sorted Arrays in Java 两个排序...
6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 ...
leetcode 316 LeetCode Summary Exclusive Time of Functions: 栈 Friend Circles:DFS Print Binary Tree:二叉树 Maximal Square:DP Maximal Rectangle:单调栈(Histogram变形) Largest Rectangle in Histogram:...
leetcode 多少题带有堆栈和队列的字梯 您将针对 Lewis Carroll ...在本作业中,您将实现一个函数word_ladder来生成这些字梯。 有许多可能的算法来生成字梯,但一个简单的算法使用堆栈和队列的组合,如
8. **Word Ladder II**:这是一个词链问题,涉及到广度优先搜索和回溯法,寻找从一个单词转换到另一个单词的最短路径。 9. **Regular Expression Matching**:这个涉及到字符串匹配和动态规划。实现一个函数来判断...
力扣 Python 解决方案Leetcode 是准备技术编码面试的...特别案例: Word Ladder II,我的 AC 代码比这个 repo 中的代码慢得多。 但是这个 repo 中的一个得到了 TLE,不知道为什么。 任何想法都将受到高度赞赏。 谢谢!
5. **搜索和回溯**:对于“NumberofIslands”、“WordLadder”等题目,应聘者必须运用搜索算法,如广度优先搜索(BFS)、深度优先搜索(DFS)以及回溯算法来寻找解决方案。 6. **动态规划和贪心算法**:...
9.1 单词变换路径(Word Ladder) 9.1.1 是否存在变换路径 9.1.2 所有最短变换路径9.2 包围区域 10. 深度优先搜索 10.1 N皇后问题 10.2 恢复IP地址 10.3 集合元素之和 10.3.1 元素可以重复 10.3.2 元素不可重复 ...
Word Ladder **知识点:** - **问题描述:** - 找到由开始到结尾的字符串的转换字符串集合,中间的转换字符串都要在给定的列表中,并且每一步只能改变一个字符。 - **解决方案分析:** - **原始方法:** - ...
例如,`WordLadder.java`可能涉及到词链问题,需要理解图的邻接矩阵或邻接表表示。 6. **字符串处理**:如模式匹配、字符串反转、最长公共前缀等。`ValidPalindrome.java`可能涉及字符串的比较和操作。 7. **位...