Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

    • Return 0 if there is no such transformation sequence.
    • All words have the same length.
    • All words contain only lowercase alphabetic characters.

最短搜索路径,所以是广度优先搜索(BFS)。

有几个问题需要注意:

1、怎样判断是否为邻居节点?

按照定义,存在一个字母差异的单词为邻居,因此采用逐位替换字母并查找字典的方法寻找邻居。

(逐个比对字典里单词也可以做,但是在这题的情况下,时间复杂度会变高,容易TLE)

2、怎样记录路径长度?

对队列中的每个单词记录路径长度。从start进入队列记作1.

长度为i的字母的邻居,如果没有访问过,则路径长度为i+1

struct Node
{
string word;
int len; Node(string w, int l): word(w), len(l) {}
}; class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
queue<Node*> q;
unordered_map<string, bool> m;
Node* beginNode = new Node(beginWord, );
q.push(beginNode);
m[beginWord] = true;
while(!q.empty())
{
Node* frontNode = q.front();
q.pop();
string frontWord = frontNode->word;
//neighbor search
for(int i = ; i < frontWord.size(); i ++)
{
for(char c = 'a'; c <= 'z'; c ++)
{
if(c == frontWord[i])
continue; string frontWordCp = frontWord;
frontWordCp[i] = c;
//end
if(frontWordCp == endWord)
return frontNode->len+;
if(wordDict.find(frontWordCp) != wordDict.end() && m[frontWordCp] == false)
{
Node* neighborNode = new Node(frontWordCp, frontNode->len+);
q.push(neighborNode);
m[frontWordCp] = true;
}
}
}
}
return ;
}
};

【LeetCode】127. Word Ladder的更多相关文章

  1. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

  2. 【leetcode】126. Word Ladder II

    题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...

  3. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  4. 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)

    原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...

  5. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  6. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  7. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  8. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】916. Word Subsets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...

随机推荐

  1. 使用Scala

    1. 净资产应用实例 我们要构建这样一个应用,它会取回一份列表,其中包括用户持有的股票的代码以及股份,并告知他们在当前日期为止的这些投资的总价.这包含了几件事:获取用户输入.读文件.解析数据.写文件. ...

  2. MSSQL工作中常用的小技巧

    大概看了一下有接近二十天自己没有写博客了,一来是因为国庆之前公司工作总会比较繁杂一点,国庆自己也需要休息,二来是因为学习一些新的东西,公司写了一天SQL回家看了看以前的笔记,感觉还挺不错,贴出来供大家 ...

  3. maven 将jar 下载到工程当前目录下

    在 pom.xml 的目录下,运行cmd命令 : call mvn -f pom.xml dependency:copy-dependencies 然后在同一目录下出现文件夹target,内容就是ja ...

  4. scala 学习笔记十 元组

    1.元组初始化 2.元组作为函数返回值 3.元组拆包 上面168行 ,单个val后面跟着一个由五个标识符构成的元组,表示对ff返回的元组进行拆包 上面174行,将整个元组捕获到单个val或var中,那 ...

  5. PHP实战 新闻管理系统 使用到了bootstrap框架

    刚刚接触 PHP 仿照视频 写了个新闻管理系统 当中也使用到了bootstrap框架 写下来整理一下思路. 这是个非常easy的系统.首先是建立数据库表. mysql>create databa ...

  6. 【Ansible】Playbook实例

    Learn to build Ansible playbooks with our guide, one step at a time In our previous posts, we introd ...

  7. [Node.js] Child Process with fork() to handle heavy calculation process

    When build server, if we have a API endpoint requires some heavy calculation process, it will block ...

  8. [Node.js]28. Level 5: Express Server

    Now let's create an express server which queries out for this search term and just returns the json. ...

  9. OSX:不同OSX版本号的标记可能不兼容-续

    不同OSX版本号的标记可能不兼容-续: 经过測试,10.10DP2的Update.俗称DP3.的版本号也没有纠正这个问题.而造成该问题的是安装过程中一開始就选择中文,假设安装时使用英文.在第一次进入操 ...

  10. hdu4497 GCD and LCM

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...