前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0126 - Word Ladder II

LeetCode 0126 - Word Ladder II

作者头像
Reck Zhang
发布于 2021-08-11 06:36:31
发布于 2021-08-11 06:36:31
18000
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Word Ladder II

Desicription

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 transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return an empty list if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output:
    [
        ["hit","hot","dot","dog","cog"],
        ["hit","hot","lot","log","cog"]
    ]

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: []

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    vector<vector<string>> findLadders(const string& beginWord, const string& endWord, vector<string>& wordList) {
        vector<vector<string>> res;
        unordered_set<string> dict(wordList.begin(), wordList.end());
        unordered_set<string> words;
        queue<vector<string>> paths;
        paths.push(vector<string>{beginWord});
        int step = 1;
        int minStep = INT_MAX;
        while(!paths.empty()) {
            vector<string> currentPath = paths.front();
            paths.pop();
            if(currentPath.size() > step) {
                for(const string &word : words)
                    dict.erase(word);
                words.clear();
                step = currentPath.size();
                if(step > minStep)
                    break;
            }
            string lastWord = currentPath.back();
            for(int i = 0; lastWord[i]; i++) {
                string tmpWord = lastWord;
                for(char ch = 'a'; ch <= 'z'; ch++) {
                    tmpWord[i] = ch;
                    if(!dict.count(tmpWord))
                        continue;
                    words.insert(tmpWord);
                    vector<string> nextPath = currentPath;
                    nextPath.push_back(tmpWord);
                    if(tmpWord == endWord)
                        res.push_back(nextPath), minStep = min(minStep, (int)nextPath.size());
                    else
                        paths.push(nextPath);
                }
            }
        }
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Word Ladder II
    • Desicription
    • Solution
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档