<p>Given a string <code>s</code> and a dictionary of strings <code>wordDict</code>, add spaces in <code>s</code> to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in <strong>any order</strong>.</p>

<p><strong>Note</strong> that the same word in the dictionary may be reused multiple times in the segmentation.</p>

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

<pre>
<strong>Input:</strong> s = &quot;catsanddog&quot;, wordDict = [&quot;cat&quot;,&quot;cats&quot;,&quot;and&quot;,&quot;sand&quot;,&quot;dog&quot;]
<strong>Output:</strong> [&quot;cats and dog&quot;,&quot;cat sand dog&quot;]
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;pineapplepenapple&quot;, wordDict = [&quot;apple&quot;,&quot;pen&quot;,&quot;applepen&quot;,&quot;pine&quot;,&quot;pineapple&quot;]
<strong>Output:</strong> [&quot;pine apple pen apple&quot;,&quot;pineapple pen apple&quot;,&quot;pine applepen apple&quot;]
<strong>Explanation:</strong> Note that you are allowed to reuse a dictionary word.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;catsandog&quot;, wordDict = [&quot;cats&quot;,&quot;dog&quot;,&quot;sand&quot;,&quot;and&quot;,&quot;cat&quot;]
<strong>Output:</strong> []
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 20</code></li>
	<li><code>1 &lt;= wordDict.length &lt;= 1000</code></li>
	<li><code>1 &lt;= wordDict[i].length &lt;= 10</code></li>
	<li><code>s</code> and <code>wordDict[i]</code> consist of only lowercase English letters.</li>
	<li>All the strings of <code>wordDict</code> are <strong>unique</strong>.</li>
	<li>Input is generated in a way that the length of the answer doesn&#39;t exceed&nbsp;10<sup>5</sup>.</li>
</ul>


class Solution
{
private:
    vector<string> res;
    struct Node
    {
        unordered_map<char, Node *> next;
        bool endNode;
        Node() : endNode(false) {}
    };

    struct Try
    {
        Node *root;
        Try() : root(new Node()) {}
    } tr;

    void insertString(string s)
    {
        Node *cur = tr.root;
        for (char i : s)
        {
            if (!cur->next.count(i))
            {
                cur->next[i] = new Node();
            }
            cur = cur->next[i];
        }
        cur->endNode = true;
    }

    bool search(string s)
    {
        Node *cur = tr.root;
        for (char i : s)
        {
            if (!cur->next.count(i))
                return false;
            cur = cur->next[i];
        }
        return cur->endNode;
    }
    void backtracking(int start, const string s, string t, Node *cur)
    {
        if (start == s.size())
        {
            res.push_back(t);
        }
        else
        {
            int n = s.size();
            for (int end = start; end < n; end++)
            {
                char c = s[end];
                if (!cur->next.count(c))
                    return;
                cur = cur->next[c];
                if (cur->endNode)
                {
                    string sCur = s.substr(start, end - start + 1);
                    backtracking(end + 1, s, t.empty() ? sCur : t + " " + sCur, tr.root);
                }
            }
        }
    }

public:
    vector<string> wordBreak(string s, vector<string> &wordDict)
    {
        for (string t : wordDict)
        {
            insertString(t);
        }
        backtracking(0, s, "", tr.root);
        return res;
    }
};

9500