Word Ladder系列

1.Word Ladder

问题描述:

给两个word(beginWord和endWord)和一个字典word list,找出从beginWord到endWord之间的长度最长的一个序列,条件:

1.字典中的每个单词只能使用一次;

2.序列中的每个单词都必须是字典中的单词;

例如:

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.

注意:

  如果找不到合适的序列,返回0;

  所有的单词长度都是一样的;

所有的单词都只由小写字母组成。

思路:

采用DFS依次遍历

代码如下:

 1 class Solution {
 2 public:
 3     int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
 4         unordered_set<string> s1 = {beginWord}; // Front end
 5         unordered_set<string> s2 = {endWord}; // Back end
 6         wordDict.erase(beginWord);
 7         wordDict.erase(endWord);
 8
 9         return ladderLength(s1, s2, wordDict, 1);
10     }
11
12 private:
13     int ladderLength(unordered_set<string>& s1, unordered_set<string>& s2, unordered_set<string>& wordDict, int level) {
14         if (s1.empty()) // We can‘t find one.
15             return 0;
16         unordered_set<string> s3; // s3 stores all words 1 step from s1.
17         for (auto word : s1) {
18             for (auto& ch : word) {
19                 auto originalCh = ch;
20                 for (ch = ‘a‘; ch <= ‘z‘; ++ ch) {
21                     if (ch != originalCh) {
22                         if (s2.count(word))  // We found one.
23                             return level + 1;
24                         if (wordDict.count(word)) {
25                             wordDict.erase(word); // Avoid duplicates.
26                             s3.insert(word);
27                         }
28                     }
29                 }
30
31                 ch = originalCh;
32             }
33         }
34         // Continue with the one with smaller size.
35         return (s2.size() <= s3.size()) ? ladderLength(s2, s3, wordDict, level + 1) : ladderLength(s3, s2, wordDict, level + 1);
36     }
37 };
时间: 2024-11-07 04:02:09

Word Ladder系列的相关文章

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

Java Word Ladder(字梯)

问题: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:

18. Word Ladder &amp;&amp; Word Ladder II

Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example,

【LeetCode】Word Ladder 字符串

题目:Word Ladder <span style="font-size:18px;">/**LeetCode word ladder * 题目:给定一个起始单词和一个终结单词以及一个字典,要求每次变换一个字符,成为字典中新的词,直到变为最后的词,要求其最短路径 * 思路:利用队列,先弹出第一个词,分别将词中每一个字符替换直到找到一个字典中存在的词,加入队列,直到匹配的词是最后一个,此时终止 * 如果没有这样的路径,则返回0 */ package javaTrain; i

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:

LeetCode OJ - Word Ladder 2

我发现在leetcode上做题,当我出现TLE问题时,往往是代码有漏洞,有些条件没有考虑到,这道题又验证了我这一想法. 这道题是在上一道的基础上进一步把所有可能得转换序列给出. 同样的先是BFS,与此同时需要一个hashMap记录下每个节点,和他所有父节点的对应关系,然后通过DFS,回溯所有可能的路径. 下面是AC代码. 1 /** 2 * Given two words (start and end), and a dictionary, find all shortest transform

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

[Leetcode][JAVA] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =