LC 127. Word Ladder (two end bfs)

Link

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> words;
        for(auto &s:wordList){
            words.insert(s);
        }
        if(words.find(endWord)==words.end()) return 0;
        unordered_set<string> beginSet;
        unordered_set<string> endSet;
        beginSet.insert(beginWord);
        endSet.insert(endWord);
        unordered_set<string> visited;
        visited.insert(beginWord);
        visited.insert(endWord);
        int level=1;
        while(!beginSet.empty() && !endSet.empty()){
            if(beginSet.size()>endSet.size()){
                auto tmp=beginSet;
                beginSet=endSet;
                endSet=tmp;
            }
            unordered_set<string> tmp;
            for(auto s:beginSet){
                for(int i=0;i<s.size();++i){
                    char bk=s[i];
                    for(char c=‘a‘;c<=‘z‘;++c){
                        if(c==bk) continue;
                        s[i]=c;
                        if(endSet.find(s)!=endSet.end()) return level+1;
                        if(visited.find(s)==visited.end() && words.find(s)!=words.end()){
                            visited.insert(s);
                            tmp.insert(s);
                        }
                    }
                    s[i]=bk;
                }
            }
            beginSet=tmp;
            ++level;
        }
        return 0;
    }
};

原文地址:https://www.cnblogs.com/FEIIEF/p/12286268.html

时间: 2025-01-09 08:56:04

LC 127. Word Ladder (two end bfs)的相关文章

[LeetCode] 126. Word Ladder II_Hard tag: BFS&amp;DFS

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 transformed word must exist in the word list. Note

127. Word Ladder(js)

127. Word Ladder 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 transformed word must exi

127. Word Ladder(M)

127. Word LadderGiven 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 transformed word must exis

Leetcode 127. Word Ladder

思路: 1 class Solution(object): 2 def __init__(self): 3 return 4 5 def ladderLength(self, beginWord, endWord, wordList): 6 """ 7 :type beginWord: str 8 :type endWord: str 9 :type wordList: List[str] 10 :rtype: int 11 """ 12 fro

leetcode 127. Word Ladder ----- java

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 li

127. Word Ladder

相当于bfs 1 public int ladderLength(String beginWord, String endWord, Set<String> wordList) { 2 if(beginWord == null || endWord == null || beginWord.length() == 0 || endWord.length() != beginWord.length() || wordList == null) { 3 return 0; 4 } 5 Set<

Java for LeetCode 127 Word Ladder

Given two words (beginWord and endWord), and a dictionary, 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 dictionary For exam

(Java) LeetCode 127. Word Ladder —— 单词接龙

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 transformed word must exist in the word li

leetcode Word Ladder II

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