<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>

class Solution {
public:
    bool is_adj(const string& w1, const string& w2) {
        int dif_count = 0;
        int n = w1.size();
        int idx = 0;
        while (idx < n && dif_count < 2) {
            dif_count += (w1[idx] != w2[idx]);
            ++idx;
        }

        return dif_count < 2;
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_map<string, vector<string>> adj_list;
        int n = wordList.size();

        for (int i = 0; i < n; ++i) {
            for (int j = i+1; j < n; ++j) {
                const auto& w1 = wordList[i];
                const auto& w2 = wordList[j];
                if (is_adj(w1, w2)) {
                    adj_list[w1].push_back(w2);
                    adj_list[w2].push_back(w1);
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            const auto& w1 = wordList[i];
            const auto& w2 = beginWord;
            if (is_adj(w1, w2)) {
                adj_list[w1].push_back(w2);
                adj_list[w2].push_back(w1);
            }
        }

        for (const auto& [key, val] : adj_list) {
            cout << "k: " << key;
            for (const auto& v : val) {
                cout << " " << v;
            }
            cout << '\n';
        }

        unordered_set<string> seen;
        queue<pair<string, int>> q;
        q.emplace(beginWord, 1);
        while (!q.empty()) {
            const auto [cur_word, cur_jumps] = q.front();
            q.pop();

            if (cur_word == endWord) return cur_jumps;

            if (seen.contains(cur_word)) continue;
            seen.insert(cur_word);

            for (const auto& adj_word : adj_list[cur_word]) {
                q.emplace(adj_word, cur_jumps+1);
            }


        }

        return 0;
    }
};


45375