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

// What's the unknown? The minimum # of words to transform beginWord to endWord, 
// given the words in wordList as possible transformations
// What's the data? We're given beginWord, endWord, wordList, where wordList may contain
// a sequence from beginWord to endWord
// What's the condition? Each adjacent transformation can only differ by single letter.
// Words in wordList are of the same length as beginWord and endWord.
// All words in wordList are unique


// If endWord is not in wordList, then return 0

// Need a function to find words one letter away from given word

// Have you seen a similar problem? Yes, problem about finding if a given word
// can be changed to a target word by removing, adding, or replacing character

// Create a class WordUsage to store word and whether word has been used in sequence.
// When recurse to word, mark word as seen; when exit stack and finish using word,
// mark word as unseen. If no more candidates for next word, stop searching.
// If curr word == endWord, record sequence length

// BUD (Bottleneck, Unnecessary, Duplicated)

class Solution {
public:
//["hot","het","dot","dog","lot","log","cog"]
//[hit]
//[hot, het]
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        queue<string> q;
        set<string> seen;

        q.push(beginWord);
        int lvl = 1;

        while(!q.empty()) {
            int q_size = q.size();
            for (int i = 0; i < q_size; i++) {
                string curr = q.front(); q.pop();

                if (curr == endWord) return lvl;

                vector<string> next_words = getNextWords(curr, wordList);

                for (string next_word : next_words) {
                    if (seen.find(next_word) == seen.end()) {
                        q.emplace(next_word);
                        seen.insert(next_word);
                    }
                }
            }

            lvl++;
        }

        return 0;
    }

    vector<string> getNextWords(string curr, vector<string> &wordList) {
        vector<string> nextWords;

        for(const string &word : wordList) {
            int diffCharsAllowance = 1;
            for (int i = 0; i < word.length(); i++) {
                if (word[i] != curr[i])
                    if (--diffCharsAllowance < 0) break;
            }

            if (diffCharsAllowance == 0) nextWords.emplace_back(word);
        }

        return nextWords;
    }

    // int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
    //     vector<string> sequence;
    //     map<string, int> wordUsages;

    //     int maxLength = 0;
    //     bool endWordExists = false;

    //     for(string word : wordList) {
    //         if (word == endWord) endWordExists = true;

    //         wordUsages[word] = INT_MAX;
    //     }

    //     if (!endWordExists) return 0;

    //     sequence.emplace_back(beginWord);

    //     ladderLengthHelper(sequence, wordUsages, maxLength, endWord);

    //     return maxLength;
    // }

    void ladderLengthHelper(vector<string> &sequence, map<string,int> &wordUsages, int &maxLength, const string &endWord) {
        vector<string> nextWords = getNextWords(wordUsages, sequence.back());
        //hot
        //dot
        //dog
        //cog
        if (nextWords.size() == 0) return;

        for (string nextWord : nextWords) {
            if (nextWord == endWord) {
                if (maxLength == 0) 
                    maxLength = sequence.size()+1;
                else 
                    maxLength = min(maxLength, (int)sequence.size()+1);

                return;
            }

            if (sequence.size()-1 < wordUsages[nextWord]) {
                sequence.emplace_back(nextWord);
                wordUsages[nextWord] = sequence.size();
                ladderLengthHelper(sequence, wordUsages, maxLength, endWord);
            }
        }
    }

    vector<string> getNextWords(const map<string,int> &wordUsages, string curr) {
        vector<string> nextWords;

        for(pair<string,bool> word_usage : wordUsages) {
            int diffCharsAllowance = 1;
            //if (!word_usage.second) {
                for (int i = 0; i < curr.length(); i++) {
                    if (curr[i] != word_usage.first[i])
                        diffCharsAllowance--;
                }
            //}
            if (diffCharsAllowance == 0) nextWords.emplace_back(word_usage.first);
        }

        return nextWords;
    }

    struct WordUsage {
        string word;
        bool used;
    };
};
30425