LeetCode-127. Word Ladderhttps://leetcode.com/problems/word-ladder/
A transformation sequence from word beginWord
to word endWord
using a dictionary wordList
is a sequence of words beginWord -> s1 -> s2 -> ... -> sk
such that:
si
for 1 <= i <= k
is in wordList
. Note that beginWord
does not need to be in wordList
.sk == endWord
Given two words, beginWord
and endWord
, and a dictionary wordList
, return the number of words in the shortest transformation sequence from beginWord
to endWord
, or 0
if no such sequence exists.
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
BFS
此处仅限于计数
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector& wordList) {
unordered_map> mp;
for(string& w:wordList) for(int i=0; i q;
q.push(beginWord);
while(!q.empty()){
ans++;
queue nq;
while(!q.empty()){
string s = q.front(); q.pop();
for(int i=0; i
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector& wordList) {
unordered_set dict(wordList.begin(), wordList.end());
if(!dict.count(endWord)) return 0;
unordered_set forward;
forward.insert(beginWord);
unordered_set backward;
backward.insert(endWord);
unordered_set next;
int steps = 0;
while (!forward.empty() && !backward.empty()) {
++steps;
if (forward.size() > backward.size()) swap(forward, backward);
for (auto &word : forward) dict.erase(word);
for (auto &word: backward) dict.erase(word);
for (string word : forward) {
for (int i = 0; i < word.length(); ++i) {
const char c = word[i];
for (int j = 'a'; j <= 'z'; ++j) {
if (c == j) continue;
word[i] = j;
if (backward.count(word)) return steps + 1; //恰好相邻
if (!dict.count(word)) continue; //都不存在
next.insert(word);
}
word[i] = c;
}
}
swap(forward, next);
next.clear();
}
return 0;
}
};
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector& wordList) {
unordered_set dict(wordList.begin(),wordList.end());
if (dict.find(endWord) == dict.end()) {return 0;}
queue q;
q.push(beginWord);
int len = 1;
while (!q.empty()){
int size = q.size();
while (size--){
string last = q.front(); q.pop();
if (last == endWord) {return len;}
dict.erase(last);
for (int i=0; i
class Solution {
public int ladderLength(String beginWord, String endWord, List wordList) {
Set dict = new LinkedHashSet<>(wordList);
if (!dict.contains(endWord)) {return 0;}
Queue q = new LinkedList();
q.offer(beginWord);
int len = 1;
while (!q.isEmpty()){
int sz = q.size();
while (sz-- > 0){
String last = q.poll();
if (last.equals(endWord)) {return len;}
dict.remove(last);
for (int i=0; i
LeetCode-126. Word Ladder II [C++][Java]_贫道绝缘子的博客-CSDN博客Given two words,beginWordandendWord, and a dictionarywordList, returnall theshortest transformation sequencesfrombeginWordtoendWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words[beginWord, https://blog.csdn.net/qq_15711195/article/details/122814072