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:

  1. Only one letter can be changed at a time
  2. 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.
  1. class Solution {
  2. public:
  3. int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
  4. int l = beginWord.length(), n = wordList.size(), i;
  5. if(l <= || n <= || beginWord == endWord)
  6. return ;
  7. queue<string> q;
  8. q.push(beginWord);
  9. map<string, int> m;
  10. m[beginWord] = ;
  11. while(!q.empty())
  12. {
  13. string s = q.front();
  14. q.pop();
  15. for(i = ; i < l; i++)
  16. {
  17. for(char c = 'a'; c <= 'z'; c++)
  18. {
  19. string t = s;
  20. t[i] = c;
  21. if(wordList.find(t) != wordList.end() && m.find(t) == m.end())
  22. {
  23. m[t] = m[s] + ;
  24. if(t == endWord)
  25. return m[t];
  26. q.push(t);
  27. }
  28. }
  29. }
  30. }
  31. return ;
  32. }
  33. };
  1. // ---------------------------
  2. // BFS non-recursive method
  3. // ---------------------------
  4. //
  5. // Using BFS instead of DFS is becasue the solution need the shortest transformation path.
  6. //
  7. // So, we can change every char in the word one by one, until find all possible transformation.
  8. //
  9. // Keep this iteration, we will find the shorest path.
  10. //
  11. // For example:
  12. //
  13. // start = "hit"
  14. // end = "cog"
  15. // dict = ["hot","dot","dog","lot","log","dit","hig", "dig"]
  16. //
  17. // +-----+
  18. // +-------------+ hit +--------------+
  19. // | +--+--+ |
  20. // | | |
  21. // +--v--+ +--v--+ +--v--+
  22. // | dit | +-----+ hot +---+ | hig |
  23. // +--+--+ | +-----+ | +--+--+
  24. // | | | |
  25. // | +--v--+ +--v--+ +--v--+
  26. // +----> dot | | lot | | dig |
  27. // +--+--+ +--+--+ +--+--+
  28. // | | |
  29. // +--v--+ +--v--+ |
  30. // +----> dog | | log | |
  31. // | +--+--+ +--+--+ |
  32. // | | | |
  33. // | | +--v--+ | |
  34. // | +--->| cog |<-- + |
  35. // | +-----+ |
  36. // | |
  37. // | |
  38. // +----------------------------------+
  39. //
  40. // 1) queue <== "hit"
  41. // 2) queue <== "dit", "hot", "hig"
  42. // 3) queue <== "dot", "lot", "dig"
  43. // 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:

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

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Return

  1. [
  2. ["hit","hot","dot","dog","cog"],
  3. ["hit","hot","lot","log","cog"]
  4. ]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  1. class Solution {
  2. public:
  3. void buildTree(string beginWord, string endWord, unordered_set<string> &wordList, map<string, unordered_set<string>> &m)
  4. {
  5. map<string, int> level;
  6. level[beginWord] = ;
  7. queue<string> q;
  8. q.push(beginWord);
  9. int lvl = ;
  10. while (!q.empty())
  11. {
  12. string s = q.front();
  13. q.pop();
  14. if (lvl && level[s] > lvl)
  15. break;
  16. bool found = false;
  17. int l = s.length(), i;
  18. for (i = ; i < l; i++)
  19. {
  20. string t = s;
  21. for (char c = 'a'; c <= 'z'; c++)
  22. {
  23. t[i] = c;
  24. if (t == endWord)
  25. {
  26. m[s].clear();
  27. m[s].insert(endWord);
  28. lvl = level[s];
  29. found = true;
  30. break;
  31. }
  32. else if (wordList.find(t) != wordList.end())
  33. {
  34. if (level.find(t) == level.end())
  35. {
  36. level[t] = level[s] + ;
  37. m[s].insert(t);
  38. q.push(t);
  39. }
  40. else if (level[t] == level[s] + )
  41. {
  42. m[s].insert(t);
  43. }
  44. }
  45. }
  46. if (found)
  47. break;
  48. }
  49. }
  50. }
  51. int countnum = ;
  52. void dfs(string endWord, vector<vector<string>> &ans, vector<string> &v, map<string, unordered_set<string>> &m, string s)
  53. {
  54. if (m.find(s) == m.end() && s == endWord)
  55. {
  56. ans.push_back(v);
  57. return;
  58. }
  59. for (unordered_set<string>::iterator it = m[s].begin(); it != m[s].end(); it++)
  60. {
  61. v.push_back(*it);
  62. dfs(endWord, ans, v, m, *it);
  63. v.pop_back();
  64. }
  65. }
  66.  
  67. vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
  68. vector<vector<string>> ans;
  69. int l = beginWord.length(), n = wordList.size();
  70. if (l <= || n <= || beginWord == endWord)
  71. return ans;
  72. map<string, unordered_set<string>> m;
  73. buildTree(beginWord, endWord, wordList, m);
  74.  
  75. vector<string> v;
  76. v.push_back(beginWord);
  77. map<string, bool> visit;
  78. dfs(endWord, ans, v, m, beginWord);
  79. return ans;
  80. }
  81.  
  82. };
  1. // Solution
  2. //
  3. // 1) Using BSF algorithm build a tree like below
  4. // 2) Using DSF to parse the tree to the transformation path.
  5. //
  6. // For example:
  7. //
  8. // start = "hit"
  9. // end = "cog"
  10. // dict = ["hot","dot","dog","lot","log","dit","hig", "dig"]
  11. //
  12. // +-----+
  13. // +-------------+ hit +--------------+
  14. // | +--+--+ |
  15. // | | |
  16. // +--v--+ +--v--+ +--v--+
  17. // | dit | +-----+ hot +---+ | hig |
  18. // +--+--+ | +-----+ | +--+--+
  19. // | | | |
  20. // | +--v--+ +--v--+ +--v--+
  21. // +----> dot | | lot | | dig |
  22. // +--+--+ +--+--+ +--+--+
  23. // | | |
  24. // +--v--+ +--v--+ |
  25. // +----> dog | | log | |
  26. // | +--+--+ +--+--+ |
  27. // | | | |
  28. // | | +--v--+ | |
  29. // | +--->| cog |<-- + |
  30. // | +-----+ |
  31. // | |
  32. // | |
  33. // +----------------------------------+

127. 126. Word Ladder *HARD* -- 单词每次变一个字母转换成另一个单词的更多相关文章

  1. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  2. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  3. 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功

    public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...

  4. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  5. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  6. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  7. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  8. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  9. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. CentOS 7一些常用配置

    一.更改启动模式 背景:个人开发环境,安装了GNOME桌面,默认启动是图形化模式. 修改为命令行模式. systemctl set-default multi-user.target 二.命令行模式下 ...

  2. Using GET_GROUP_SELECTION For Record Groups in Oracle Forms

    Retrieves the sequence number of the selected row for the given group. Suppose you want to get a par ...

  3. C#窗体->>随机四则运算

    用户需求: 程序能接收用户输入的整数答案,并判断对错程序结束时,统计出答对.答错的题目数量.补充说明:0——10的整数是随机生成的用户可以选择四则运算中的一种用户可以结束程序的运行,并显示统计结果.在 ...

  4. [Effective Java]第五章 泛型

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. 软/硬链接指令:ln

    语法: ln  [选项]  原文件  目标文件 选项: -s 创建软连接(创建软链接时,若所在文件夹不一致,原文件要使用绝对路径) 硬链接特征: 1.拥有相同i节点和存储block块,可以看成是同一个 ...

  6. const变量赋值报错分析

    const变量赋值报错分析 const变量赋值报错 从变量到常量的赋值是合法C++的语法约定的, 如从char 到const char顺畅: 但从char **到 const char **编译器就会 ...

  7. javascript权威指南笔记--javascript语言核心(二)

    1.函数作用域:在函数内声明的所有变量在函数体内始终是可见的.这意味着在变量声明之前甚至已经可用. *“声明提前”:javascript函数里声明的所有变量(但不涉及赋值)都被提前至函数的顶部. fu ...

  8. 《Linux内核设计的艺术》学习笔记(四)默认段和偏移寄存器

    参考书籍:<Intel微处理器> 表1 默认的16位段 + 偏移寻址组合 段 偏移量 特殊用途 CS IP 指令地址 SS SP或BP 堆栈地址 DS BX.DI.SI.8位或16位数 数 ...

  9. iOS - Swift NSNumber 数字

    前言 public class NSNumber : NSValue public class NSDecimalNumber : NSNumber NSNumber 可以被赋值为各种数值类型.我们可 ...

  10. hibernate对象关系实现(一)一对多

    hibernate是对jdk一个封装工具,实现对象和数据库之间数据映射.使用时涉及到四个问题:a.对象之间的关系在类中的体现:b,对象关系对应的数据库中表之间体现:c.实现a,b在hibernate的 ...