<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
{
    static constexpr int s_iMaxWordLen = 10;
    static constexpr int s_iLettersNum = 26;

    struct SGraphNode
    {
        inline void AddNeighNode(short sNodeIdx)
        {
            sNeighNode[ucNeighNum++] = sNodeIdx;
        }

        unsigned char ucNeighNum = 0;
        short         sNeighNode[(s_iLettersNum-1)*s_iMaxWordLen];
    };

public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList)
    {
        sort(wordList.begin(), wordList.end());

        auto itBegin = lower_bound(wordList.begin(), wordList.end(), beginWord);
        if(itBegin == wordList.end() || *itBegin != beginWord)
            itBegin = wordList.insert(itBegin, beginWord);

        auto itEnd = lower_bound(wordList.begin(), wordList.end(), endWord);
        if(itEnd == wordList.end() || *itEnd != endWord)
            return 0;

        const short sBeginWordIdx = itBegin - wordList.begin();
        const short sEndWordIdx   = itEnd   - wordList.begin();
        const int   iWordsNum     = (int)wordList.size();
        const int   iWordLen      = (int)beginWord.length();

        vector<SGraphNode> vecGraph;
        vector<short>      vecWordsInfo;
        vecGraph    .resize(iWordsNum);
        vecWordsInfo.resize(iWordsNum);

        for(int i = 0; i < iWordsNum; i++)
            vecWordsInfo[i] = i;

        // Create graph of words.
        for(int i = iWordLen-1; i >= 0; i--)
        {
            const int iNextIdx = i + 1;
            auto CompareWordInfo = [&](const short &left, const short &right)
            {
                const char *cLeftWord  = wordList[left ].c_str();
                const char *cRightWord = wordList[right].c_str();
                const int   iCmpRes    = strncmp(cLeftWord + iNextIdx, cRightWord + iNextIdx, iWordLen-iNextIdx);

                if     (iCmpRes < 0) return true;
                else if(iCmpRes > 0) return false;
                else                 return (strncmp(cLeftWord, cRightWord, iNextIdx) < 0) ? true : false;
            };
            if(i < iWordLen-1)
                sort(vecWordsInfo.begin(), vecWordsInfo.end(), CompareWordInfo);

            int iStartIdx = 0, iEndIdx = 0;
            while(iStartIdx < iWordsNum)
            {
                do { iEndIdx++; }
                while(iEndIdx < iWordsNum && 
                      strncmp(wordList[vecWordsInfo[iStartIdx]].c_str(),            wordList[vecWordsInfo[iEndIdx]].c_str(),            i                ) == 0 && 
                      strncmp(wordList[vecWordsInfo[iStartIdx]].c_str() + iNextIdx, wordList[vecWordsInfo[iEndIdx]].c_str() + iNextIdx, iWordLen-iNextIdx) == 0);

                for(int j = iStartIdx; j < iEndIdx-1; j++)
                {
                    for(int k = j+1; k < iEndIdx; k++)
                    {
                        const short sIdx1 = vecWordsInfo[j];
                        const short sIdx2 = vecWordsInfo[k];
                        vecGraph[sIdx1].AddNeighNode(sIdx2);
                        vecGraph[sIdx2].AddNeighNode(sIdx1);
                    }
                }

                iStartIdx = iEndIdx;
            }
        }

        // Wave search in graph
        queue<short>                queWave;
        unordered_map<short, short> hashInQueue;
        //queWave    .reserve(iWordsNum);
        hashInQueue.reserve(iWordsNum);
        queWave.push(sBeginWordIdx);
        hashInQueue.insert({ sBeginWordIdx, -1 });

        while(!queWave.empty())
        {
            const short sWordIdx = queWave.front();
            if(sWordIdx == sEndWordIdx)
                break;

            queWave.pop();
            for(unsigned char ucNeigh = 0; ucNeigh < vecGraph[sWordIdx].ucNeighNum; ucNeigh++)
            {
                const short sNeighIdx = vecGraph[sWordIdx].sNeighNode[ucNeigh];
                if(!hashInQueue.contains(sNeighIdx))
                {
                    queWave.push(sNeighIdx);
                    hashInQueue.insert({ sNeighIdx, sWordIdx });
                }
            }
        }

        // Traverse path to start word.
        int   iChainLen = 0;
        short sCurrIdx  = sEndWordIdx;
        while(true)
        {
            const auto it = hashInQueue.find(sCurrIdx);
            if(it != hashInQueue.end())
            {
                sCurrIdx = it->second;
                iChainLen++;
            }
            else
                break;
        }

        return iChainLen;
    }
};

29775