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

struct Trie {
    // TODO: Maybe I only need one word per node, not a vector.
    //       the kattis code has the same TODO comment.
    void insert(const string& word, const int id) {
        shared_ptr<TrieNode> curr = root;
        for (const char c : word) {
            auto& node = curr->children[c - 'a'];
            if (!node)
                node = make_shared<TrieNode>();
            curr = node;
        }
        curr->word_end_ids.push_back(id);
    }

    vector<int> find_neighbors(const string& word) {
        if (memo.count(word))
            return memo[word];

        vector<int> res;
        shared_ptr<TrieNode> curr = root;

        for (int i = 0; i < (int)word.size(); i++) {
            const auto node = curr->children[word[i] - 'a'];

            for (auto other : curr->children) {
                if (other == node)
                    continue;

                for (int j = i + 1; j < (int)word.size() && other; j++)
                    other = other->children[word[j] - 'a'];

                if (other)
                    for (const auto x : other->word_end_ids)
                        res.emplace_back(x);
            }

            curr = node;
        }

        return memo[word] = res;
    }

private:
    struct TrieNode {
        array<shared_ptr<TrieNode>, 26> children;
        vector<int> word_end_ids;
    };

    shared_ptr<TrieNode> root = make_shared<TrieNode>();
    unordered_map<string, vector<int>> memo;
};

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& words) {
        words.emplace_back(beginWord);
        Trie trie;
        // TODO: i dont need the index. Restructure it.
        // or maybe I do need it... for the BFS indices???????
        for (int i = 0; i < (int)words.size(); i++) {
            trie.insert(words[i], i);
        }

        // TODO: Maybe transform the vector to a set, that way I can
        //       do some nice refactoring.

        int to = -1;

        for (int i = 0; i < (int)words.size(); i++) {
            if (words[i] == endWord) {
                to = i;
                break;
            }
        }
        if (to == -1)
            return 0;

        const int from = words.size() - 1;

        queue<pair<int, int>> q;
        q.emplace(from, 1);
        vector<bool> visited(words.size(), false);

        while (!q.empty()) {
            const auto [u, dist] = q.front();
            q.pop();

            if (u == to)
                return dist;

            for (const int v : trie.find_neighbors(words[u])) {
                if (visited[v])
                    continue;
                q.emplace(v, dist + 1);
                visited[v] = u;
            }
        }

        return 0;
    }
};
62925