126. 单词接龙 II

126. 单词接龙 II

题目描述

给定两个单词(beginWordendWord)和一个字典 wordList,找出所有从 beginWordendWord 的最短转换序列。转换需遵循如下规则:

  1. 每次转换只能改变一个字母。
  2. 转换后得到的单词必须是字典中的单词。

说明:

  • 如果不存在这样的转换序列,返回一个空列表。

  • 所有单词具有相同的长度。

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

  • 字典中不存在重复的单词。

  • 你可以假设 beginWordendWord 是非空的,且二者不相同。

示例 1:

输入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

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

示例 2:

输入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

输出: []

解释: endWord "cog" 不在字典中,所以不存在符合要求的转换序列。
题解:

单词接龙 的进阶版本,保存转换路径。

单词接龙 中,我们已经通过 BFS 找到最短路径,下面就是根据最短路径通过 DFS 搜索所有可能的路径。

因为在 BFS 中我们记录了节点间的转换次数关系,所以在爆搜的时候可以有效利用这个状态,假设最小转换次数为 min_steps,从 s 开始进行搜索,遍历从 s 出发点的相邻节点 e ,那么可以进行剪枝:

  • 如果满足 dis[s] + 1 == dis[e] && (dis[e] < min_steps || dis[e] == min_steps && e == ed) ,则继续进行搜索

时间复杂度: O ( 2 n ∗ n ∗ L ) O(2^n*n*L) O(2nnL)

额外空间复杂度: O ( n 2 ∗ L ) O(n^2 * L) O(n2L)

class Solution {
public:
    unordered_map<string, vector<string>> g;
    unordered_set<string> cand;
    unordered_map<string, int> dis;
    vector<vector<string>> ret;
    vector<string> path;

    void getNexts( const string& st, const vector<string>& lists ) {
        string s;
        if ( cand.find( st ) == cand.end() ) {
            s = st;
            for ( int i = 0; st[i]; ++i ) {
                for ( s[i] = 'a'; s[i] <= 'z'; ++s[i] ) {
                    if ( s[i] != st[i] && cand.find(s) != cand.end() )
                        g[st].emplace_back( s );
                }
                s[i] = st[i];
            }
        }
        for( const auto& it : lists ) {
            s = it;
            for ( int i = 0; it[i]; ++i ) {
                for ( s[i] = 'a'; s[i] <= 'z'; ++s[i] ) {
                    if ( s[i] != it[i] && cand.find(s) != cand.end() )
                        g[it].emplace_back( s );
                }
                s[i] = it[i];
            }
        }
    }

    int bfs( const string& st, const string& ed ) {
        queue<string> q;
        q.push( st );
        dis[st] = 0;
        string now;
        while ( q.size() ) {
            now = q.front();
            q.pop();
            for( const auto& it : g[now] ) {
                if ( !dis.count(it) ) {
                    dis[it] = dis[now] + 1;
                    q.push( it );
                }
            }
        }
        return dis[ed];
    }

    void dfs( const string& ed, int min_steps ) {
        string s = path.back();
        if ( s == ed ) {
            ret.emplace_back( path );
            return;
        } 
        for( const auto& it : g[s] ) {
            if( dis[s] + 1 == dis[it] && ( dis[it] < min_steps || dis[it] == min_steps && it == ed ) ) {
                path.emplace_back( it );
                dfs( ed, min_steps );
                path.pop_back();
            }
        }
    }

    vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
        for ( const auto& it : wordList )
            cand.insert( it );
        if ( cand.find(endWord) == cand.end() ) return ret;

        getNexts( beginWord, wordList );

        int min_steps = bfs( beginWord, endWord );

        path.emplace_back( beginWord );
        dfs( endWord, min_steps );

        return ret;
    }
};
/*
时间:276ms,击败:68.45%
内存:28.5MB,击败:52.58%
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值