127.ワードソリティア

127.ワードソリティア

タイトル説明

辞書wordList単語からbeginWord変換シーケンスは、以下の仕様に従って形成された配列です。endWord

  • シーケンスの最初の単語はbeginWordです。

  • シーケンスの最後の単語はendWordです。

  • 変換ごとに変更できるのは1文字のみです。

  • 単語変換プロセスの途中は、単語の辞書wordListである必要があります。

次の2つの単語を与えるbeginWordendWordして辞書wordListから見つけるbeginWord最小変換シーケンスの単語の数そのような変換シーケンスがない場合は、0が返されます。endWord

例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≤beginWorld。長さ≤101\ le beginWord.length \ le 101B E G iがN W O R D l e n g t h1 0
  • endWord.length == beginWord.length
  • 1≤wordList。長さ≤50001\ le wordList.length \ le 50001W O R D L I S T l e n g t h5 0 0 0
  • wordList [i] .length == beginWord.length
  • beginWord、endWord、wordList [i]は小文字の英字で構成されています
  • beginWord!= endWord
  • wordList内のすべての文字列が互いに異なる

回答:

マップの作成+ワイド検索。

先に前処理のbeginWordwordList次の可能な各文字列は、文字列を変換して、図に基づいて構築された後。BFS検索するには、最短経路をすることができます。beginWordendWord

時間計算量:O(n ∗ L 2)O(n * L ^ 2)O nL2

余分なスペースの複雑さ:O(n 2 ∗ L)O(n ^ 2 * L)O n2L

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

    void getNexts( const string& st, const vector<string>& wordList ) {
    
    
        string 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 : wordList ) {
    
    
            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;
        unordered_set<string> vis;
        q.push( st );
        dis[st] = 1;
        vis.insert( st );
        string now;
        while ( q.size() ) {
    
    
            now = q.front();
            q.pop();
            for ( const auto& it : g[now] ) {
    
    
                if ( vis.find( it ) != vis.end() ) continue;
                dis[it] = dis[now] + 1;
                if ( it == ed ) return dis[it];
                vis.insert( it );
                q.push( it );
            }
        }
        return 0;
    }

    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
    
    
        for( const auto& it : wordList )
            cand.insert( it );
        if ( cand.find( endWord ) == cand.end() ) return 0;
        getNexts( beginWord, wordList );
        return bfs( beginWord, endWord );
    }
};
/*
时间:200ms,击败:51.20%
内存:30.8MB,击败:22.13%
*/

画像を作成せずに直接検索することもできます。

時間計算量:O(n ∗ L 2)O(n * L ^ 2)O nL2

余分なスペースの複雑さ:O(n ∗ L)O(n * L)O nL

class Solution {
    
    
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
    
    
        unordered_set<string> cand;
        unordered_map<string, int> dis;
        for( const auto& it : wordList )
            cand.insert( it );
        if ( cand.find( endWord ) == cand.end() ) return 0;
        queue<string> q;
        q.push( beginWord );
        dis[beginWord] = 1;
        
        string now, ans;
        while ( q.size() ) {
    
    
            now = q.front();
            q.pop();
            ans = now;
            for ( int i = 0; now[i]; ++i ) {
    
    
                for ( ans[i] = 'a'; ans[i] <= 'z'; ++ans[i] ) {
    
    
                    if ( ans[i] != now[i] && cand.find( ans ) != cand.end() ) {
    
    
                        if ( dis.count( ans ) ) continue;
                        dis[ans] = dis[now] + 1;
                        if ( ans == endWord ) return dis[ans];
                        q.push( ans );
                    }
                }
                ans[i] = now[i];
            }
        }
        return 0;
    }
};
/*
时间:128ms,击败:75.34%
内存:15.5MB,击败:36.19%
*/

おすすめ

転載: blog.csdn.net/MIC10086/article/details/113897277