127. 126. Word Ladder *HARD* -- 单词每次变一个字母转换成另一个单词
127.
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.
class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
int l = beginWord.length(), n = wordList.size(), i;
if(l <= || n <= || beginWord == endWord)
return ;
queue<string> q;
q.push(beginWord);
map<string, int> m;
m[beginWord] = ;
while(!q.empty())
{
string s = q.front();
q.pop();
for(i = ; i < l; i++)
{
for(char c = 'a'; c <= 'z'; c++)
{
string t = s;
t[i] = c;
if(wordList.find(t) != wordList.end() && m.find(t) == m.end())
{
m[t] = m[s] + ;
if(t == endWord)
return m[t];
q.push(t);
}
}
}
}
return ;
}
};
// ---------------------------
// BFS non-recursive method
// ---------------------------
//
// Using BFS instead of DFS is becasue the solution need the shortest transformation path.
//
// So, we can change every char in the word one by one, until find all possible transformation.
//
// Keep this iteration, we will find the shorest path.
//
// For example:
//
// start = "hit"
// end = "cog"
// dict = ["hot","dot","dog","lot","log","dit","hig", "dig"]
//
// +-----+
// +-------------+ hit +--------------+
// | +--+--+ |
// | | |
// +--v--+ +--v--+ +--v--+
// | dit | +-----+ hot +---+ | hig |
// +--+--+ | +-----+ | +--+--+
// | | | |
// | +--v--+ +--v--+ +--v--+
// +----> dot | | lot | | dig |
// +--+--+ +--+--+ +--+--+
// | | |
// +--v--+ +--v--+ |
// +----> dog | | log | |
// | +--+--+ +--+--+ |
// | | | |
// | | +--v--+ | |
// | +--->| cog |<-- + |
// | +-----+ |
// | |
// | |
// +----------------------------------+
//
// 1) queue <== "hit"
// 2) queue <== "dit", "hot", "hig"
// 3) queue <== "dot", "lot", "dig"
// 4) queue <== "dog", "log"
126.
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 intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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:
void buildTree(string beginWord, string endWord, unordered_set<string> &wordList, map<string, unordered_set<string>> &m)
{
map<string, int> level;
level[beginWord] = ;
queue<string> q;
q.push(beginWord);
int lvl = ;
while (!q.empty())
{
string s = q.front();
q.pop();
if (lvl && level[s] > lvl)
break;
bool found = false;
int l = s.length(), i;
for (i = ; i < l; i++)
{
string t = s;
for (char c = 'a'; c <= 'z'; c++)
{
t[i] = c;
if (t == endWord)
{
m[s].clear();
m[s].insert(endWord);
lvl = level[s];
found = true;
break;
}
else if (wordList.find(t) != wordList.end())
{
if (level.find(t) == level.end())
{
level[t] = level[s] + ;
m[s].insert(t);
q.push(t);
}
else if (level[t] == level[s] + )
{
m[s].insert(t);
}
}
}
if (found)
break;
}
}
}
int countnum = ;
void dfs(string endWord, vector<vector<string>> &ans, vector<string> &v, map<string, unordered_set<string>> &m, string s)
{
if (m.find(s) == m.end() && s == endWord)
{
ans.push_back(v);
return;
}
for (unordered_set<string>::iterator it = m[s].begin(); it != m[s].end(); it++)
{
v.push_back(*it);
dfs(endWord, ans, v, m, *it);
v.pop_back();
}
} vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
vector<vector<string>> ans;
int l = beginWord.length(), n = wordList.size();
if (l <= || n <= || beginWord == endWord)
return ans;
map<string, unordered_set<string>> m;
buildTree(beginWord, endWord, wordList, m); vector<string> v;
v.push_back(beginWord);
map<string, bool> visit;
dfs(endWord, ans, v, m, beginWord);
return ans;
} };
// Solution
//
// 1) Using BSF algorithm build a tree like below
// 2) Using DSF to parse the tree to the transformation path.
//
// For example:
//
// start = "hit"
// end = "cog"
// dict = ["hot","dot","dog","lot","log","dit","hig", "dig"]
//
// +-----+
// +-------------+ hit +--------------+
// | +--+--+ |
// | | |
// +--v--+ +--v--+ +--v--+
// | dit | +-----+ hot +---+ | hig |
// +--+--+ | +-----+ | +--+--+
// | | | |
// | +--v--+ +--v--+ +--v--+
// +----> dot | | lot | | dig |
// +--+--+ +--+--+ +--+--+
// | | |
// +--v--+ +--v--+ |
// +----> dog | | log | |
// | +--+--+ +--+--+ |
// | | | |
// | | +--v--+ | |
// | +--->| cog |<-- + |
// | +-----+ |
// | |
// | |
// +----------------------------------+
127. 126. Word Ladder *HARD* -- 单词每次变一个字母转换成另一个单词的更多相关文章
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 126. Word Ladder II(hard)
126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...
- 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功
public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Leetcode#126 Word Ladder II
原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- thinkphp的自动完成功能说明
手册里有一句话很关键: 自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理. 这句话说明自动完成发生的时间是create()组建数 ...
- github 修改fork的代码之后如何提交代码并pull request
官方的解释还是有点模糊,我是参照这篇文章来的. http://www.linuxidc.com/Linux/2012-12/76922.htm 关于Git的版本管理的原理,我是从这篇文章里面学习的. ...
- JAVA开发--[二维码名片生成系统]
上个月学校有个软件创新杯,最近看了网上很火的二维码比较不错.参考了国内国外一些技术文章,发现国外的确实好很多. 用的是QRcode包来实现的,基本常见的功能全部实现了. 因为刚学2个月,部分做得不是很 ...
- 购买vps创建账号后无法登录ftp
购买了vps,并在后台已经开通了ftp账号,但是前台无法登录.错误提示530.解决方法是: 请检查您ftp账号密码是否输入正确,若确认正确,请您ssh登陆服务器,然后执行sh /www/wdlinux ...
- JAVA运算符和优先级
1.算术运算符: ++ 和 -- 既可以出现在操作数的左边,也可以出现在右边,但结果是不同,如: ①int a=5: int b=a++: #先把a赋给b,a再自增 ②int a=5: int b=+ ...
- CI实践_Android持续集成
之前已经实现了Android的持续集成,并在项目中应用了一段时间.恰逢现在有几分钟时间,把之前的一些零散的点滴记录和整理一下,供有需要的朋友参考,或后续复用. 需要的准备知识:gitlab.Jenki ...
- [转载] 深入理解Linux修改hostname
原文: http://www.cnblogs.com/kerrycode/p/3595724.html 当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问 ...
- [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)
本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...
- linux学习笔记2-命令总结5
压缩解压命令 bzip2,gunzip,gzip,tar,zip 网络命令 ifconfig - 查看和配置网卡 lastlog - 检查某特定用户上次登录的时间 last - 列出目前和过去登入系统 ...
- 对SIGQUIT的实验 & Java dump
写了一个Java程序,sleep 20秒. package com.company; public class Main { public static void main(String[] args ...