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. python2 和 python3兼容写法

    一:使用ImportError,Python3中将一些Python2的模块名称做了修改,需要我们做一些处理来保证代码在不同Python版本中能够正常运行 # -*- coding: utf- -*- ...

  2. 说说CSS样式中你不知道的“大于号”

    继承在一定程度上让程序在编写的过程中更加方便,但是有时候也会给我们的程序带来一定的困扰,所以认真的学习继承的原理,以及处理的方法很重要.下面是Css中处理继承的一个方法.在一段CSS代码中见到一个大于 ...

  3. 浅谈jQuery easyui datagrid操作单元格样式

    今天项目上遇到问题,就是表格风格统一的问题,由于用了2个不同的框架,所以如果要大修比较麻烦,考虑到修改表格样式工作量会少很多,所以考虑修改jQuery easyui datagrid数据网格的样式. ...

  4. [置顶] Android异步加载数据库和多线程编程

    Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发.尚未有统一中文名称,中国大陆地区较多人使用“安卓” ...

  5. WebApi的缺点

    post只能传递对象 必须要参数一致,不能同名称

  6. 宏定义偷懒型set,get

    之前看到有这么一个写法 #define DEF_SET_GET(varType,varName,funName) \ private : varType varName; \ public : voi ...

  7. redis 超时失效key 的监听触发

    1. 事件通过 Redis 的订阅与发布功能(pub/sub)来进行分发,故需要订 阅 __keyevent@0__:expired 通道 0表示db0 根据自己的dbindex选择合适的数字 2.  ...

  8. ZOJ 2320 Cracking' RSA

    其次布尔线性方程组,高斯消元.这道题目的关键部分是看的神牛watashi的思路.另附上watashi的思路 我把他的java模板翻译成了C++的了...存起来以后当模板用...a[i][j]表示第i个 ...

  9. [置顶] macbook 深度休眠和待机

    开发用了macbook pro, 10.8.3, 因为用windows的习惯,一直比较习惯不关机,直接休眠,不是待机standby,今天找到了一个工具,可以实现,亲测通过. 下载:https://gi ...

  10. GraphQL返回分页对象

    private GraphQLOutputType testUserOutputType; private GraphQLOutputType pageType; private void initO ...