<p>A <strong>transformation sequence</strong> from word <code>beginWord</code> to word <code>endWord</code> using a dictionary <code>wordList</code> is a sequence of words <code>beginWord -&gt; s<sub>1</sub> -&gt; s<sub>2</sub> -&gt; ... -&gt; s<sub>k</sub></code> such that:</p>

<ul>
	<li>Every adjacent pair of words differs by a single letter.</li>
	<li>Every <code>s<sub>i</sub></code> for <code>1 &lt;= i &lt;= k</code> is in <code>wordList</code>. Note that <code>beginWord</code> does not need to be in <code>wordList</code>.</li>
	<li><code>s<sub>k</sub> == endWord</code></li>
</ul>

<p>Given two words, <code>beginWord</code> and <code>endWord</code>, and a dictionary <code>wordList</code>, return <em>the <strong>number of words</strong> in the <strong>shortest transformation sequence</strong> from</em> <code>beginWord</code> <em>to</em> <code>endWord</code><em>, or </em><code>0</code><em> if no such sequence exists.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> beginWord = &quot;hit&quot;, endWord = &quot;cog&quot;, wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]
<strong>Output:</strong> 5
<strong>Explanation:</strong> One shortest transformation sequence is &quot;hit&quot; -&gt; &quot;hot&quot; -&gt; &quot;dot&quot; -&gt; &quot;dog&quot; -&gt; cog&quot;, which is 5 words long.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> beginWord = &quot;hit&quot;, endWord = &quot;cog&quot;, wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The endWord &quot;cog&quot; is not in wordList, therefore there is no valid transformation sequence.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= beginWord.length &lt;= 10</code></li>
	<li><code>endWord.length == beginWord.length</code></li>
	<li><code>1 &lt;= wordList.length &lt;= 5000</code></li>
	<li><code>wordList[i].length == beginWord.length</code></li>
	<li><code>beginWord</code>, <code>endWord</code>, and <code>wordList[i]</code> consist of lowercase English letters.</li>
	<li><code>beginWord != endWord</code></li>
	<li>All the words in <code>wordList</code> are <strong>unique</strong>.</li>
</ul>

constexpr uint32_t g_max_distance = 999999;

// Indices of adjacent nodes for each node.
using GraphConnectivity = std::vector<std::vector<uint32_t>>;

// Minimal distance to each graph node.
using GraphNodesDistance = std::vector<uint32_t>;

class Solution {
public:
    int ladderLength(string beginWord, string endWord,
                     vector<string>& wordList) {

        GraphConnectivity connectivity;
        connectivity.resize(wordList.size());

        if (wordList.size() <= 64) {

            for (size_t i = 0; i + 1< wordList.size(); ++i) {

                const std::string& src_word = wordList[i];

                for (size_t j = i + 1; j < wordList.size(); ++j) {

                    const std::string& dst_word = wordList[j];

                    uint32_t letter_diff = 0;
                    for (size_t x = 0; x < src_word.size(); ++x)
                        if (src_word[x] != dst_word[x]) {
                            ++letter_diff;
                            if (letter_diff >= 2)
                                goto next_pair_check;
                        }

                    if (letter_diff == 1)
                    {
                        connectivity[i].push_back(j);
                        connectivity[j].push_back(i);
                    }
                next_pair_check:;
                }
            }
        } else {

            std::unordered_map<std::string_view, uint32_t> words_map;
            for (size_t i = 0; i < wordList.size(); ++i)
                words_map.emplace(wordList[i], uint32_t(i));

            std::string temp_word;
            for (size_t i = 0; i < wordList.size(); ++i) {

                const std::string& src_word = wordList[i];

                temp_word = src_word;
                for (size_t x = 0; x < src_word.size(); ++x) {
                    for (char c = 'a'; c <= 'z'; ++c) {
                        temp_word[x] = c;
                        if (const auto it = words_map.find(temp_word);
                            it != words_map.end())
                            connectivity[i].push_back(it->second);
                    }
                    temp_word[x] = src_word[x];
                }
            }
        }

        size_t end_word_index = wordList.size();

        uint32_t start_word_letter_diff = g_max_distance;
        for (size_t i = 0; i < wordList.size(); ++i) {

            if (wordList[i] == endWord)
                end_word_index = i;

            uint32_t letter_diff = 0;
            for (size_t x = 0; x < beginWord.size(); ++x)
                if (wordList[i][x] != beginWord[x])
                    ++letter_diff;

            if (letter_diff < start_word_letter_diff)
                start_word_letter_diff = letter_diff;
        }

        if (end_word_index >= wordList.size())
            return 0;

        GraphNodesDistance nodes_distance;
        nodes_distance.resize(wordList.size(), g_max_distance);

        for (size_t i = 0; i < wordList.size(); ++i) {

            uint32_t letter_diff = 0;
            for (size_t x = 0; x < beginWord.size(); ++x)
                if (wordList[i][x] != beginWord[x])
                    ++letter_diff;

            if (letter_diff == start_word_letter_diff) {
                nodes_distance[i] = start_word_letter_diff;
                Search_r(i, end_word_index, connectivity, nodes_distance);
            }
        }

        const uint32_t distance = nodes_distance[end_word_index];
        if (distance >= g_max_distance)
            return 0;

        return distance + 1u;
    }

    void Search_r(const size_t prev_word_index, const size_t dst_word_index,
                  const GraphConnectivity& connectivity,
                  GraphNodesDistance& nodes_distance) {

        const uint32_t next_word_distance =
            1u + nodes_distance[prev_word_index];

        if (next_word_distance >= nodes_distance[dst_word_index])
            return;

        for (size_t word_index : connectivity[prev_word_index]) {
            if (nodes_distance[word_index] <= next_word_distance)
                continue;
            nodes_distance[word_index] = next_word_distance;
            if (word_index == dst_word_index)
                continue;
            Search_r(word_index, dst_word_index, connectivity, nodes_distance);
        }
    }
};
27175