127. 单词接龙(双向BFS)

字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk:

每一对相邻的单词只差一个字母。
 对于 1 <= i <= k 时,每个 si 都在 wordList 中。注意, beginWord 不需要在 wordList 中。
sk == endWord
给你两个单词 beginWord 和 endWord 和一个字典 wordList ,返回 从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0 。

 
示例 1:

输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
输出:5
解释:一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的长度 5。
示例 2:

输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
输出:0
解释:endWord "cog" 不在字典中,所以无法进行转换。
 

提示:

1 <= beginWord.length <= 10
endWord.length == beginWord.length
1 <= wordList.length <= 5000
wordList[i].length == beginWord.length
beginWord、endWord 和 wordList[i] 由小写英文字母组成
beginWord != endWord
wordList 中的所有字符串 互不相同

单向版本,时间152ms

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> wordlist(wordList.begin(), wordList.end());
        if (wordlist.count(endWord) == 0)
            return 0;
        queue<string> q;
        q.push(beginWord);
        unordered_set<string> flag;
        int step = 1;
        while (!q.empty()) {
            int size = q.size();
            while (size--) {
                string word = q.front();
                q.pop();
                for (int i = 0; i < word.size(); i++) {
                    for (int j = 'a'; j <= 'z'; j++) {
                        string temp = word;
                        if (j != word[i]) {
                            temp[i] = j;
                            if (wordlist.count(temp) == 0)
                                continue;
                            if (temp == endWord) {
                                return step + 1;
                            }
                            if (flag.count(temp) == 0) {
                                flag.insert(temp);
                                q.push(temp);
                            }
                        }
                    }
                }
            }
            step++;
        }
        return 0;
    }
};

双向版本,时间28ms 

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> wordlist(wordList.begin(), wordList.end());
        if (wordlist.count(endWord) == 0)
            return 0;
        queue<string> front;
        queue<string> back;
        front.push(beginWord);
        back.push(endWord);
        unordered_map<string, int> front_flag;
        unordered_map<string, int> back_flag;
        front_flag.insert(make_pair(beginWord, 1));
        back_flag.insert(make_pair(endWord, 1));
        while (!front.empty() && !back.empty()) {
            int t = -1;
            if (front.size() <= back.size()) {
                t = update(front, front_flag, back_flag, wordlist);
            }
            else {
                t = update(back, back_flag, front_flag, wordlist);
            }
            if (t != -1)
                return t;
        }
        return 0;
    }

    int update(queue<string>& q, unordered_map<string, int>& flag, unordered_map<string, int>& other_flag, unordered_set<string>& wordlist) {
        int size = q.size();
        while (size--) {
            string word = q.front();
            q.pop();
            for (int i = 0; i < word.size(); i++) {
                for (int j = 'a'; j <= 'z'; j++) {
                    string temp = word;
                    if (j != word[i]) {
                        temp[i] = j;
                        if (wordlist.count(temp) == 0)
                            continue;
                        if (other_flag.count(temp) != 0) {
                            return flag[word] + other_flag[temp];
                        }
                        if (flag.count(temp) == 0) {
                            flag.insert(make_pair(temp, flag[word] + 1));
                            q.push(temp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值