<p>Given a string <code>s</code> and a dictionary of strings <code>wordDict</code>, return <code>true</code> if <code>s</code> can be segmented into a space-separated sequence of one or more dictionary words.</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;leetcode&quot;, wordDict = [&quot;leet&quot;,&quot;code&quot;]
<strong>Output:</strong> true
<strong>Explanation:</strong> Return true because &quot;leetcode&quot; can be segmented as &quot;leet code&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;applepenapple&quot;, wordDict = [&quot;apple&quot;,&quot;pen&quot;]
<strong>Output:</strong> true
<strong>Explanation:</strong> Return true because &quot;applepenapple&quot; can be segmented as &quot;apple pen apple&quot;.
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> false
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 300</code></li>
	<li><code>1 &lt;= wordDict.length &lt;= 1000</code></li>
	<li><code>1 &lt;= wordDict[i].length &lt;= 20</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>
</ul>

/*

find prefix s(i)
-> no words with prefix in dict : i++
-> yes prefix : add words w prefix
--> iterate over words w the prefix to find match
---> i++ -> prefix still valid : repeat
---> if word -> save it 
---> else -> not valid 

-> delete valid words from string and repeat for remaining string
-> if get to the end then true 
-> if cant, try deleting a different valid word
-> no valid words and cant reach end -> false

canBreak(s(0,k)) = canBreak(s(0, i)) && canBreak(s(i+1, k)) for some 0 <= i < k

*/

class Trie
{
public:
    struct TrieNode{
        bool word = false;
        unordered_map<char, TrieNode*> children;
    };
    
    TrieNode* root;
    Trie()
    {
        root = new TrieNode;
    }

    void addWord(string &word)
    {
        TrieNode* curr = root;

        for (auto c : word)
        {
            if (curr->children.find(c) == curr->children.end())
            {
                curr->children[c] = new TrieNode;
            }

            curr = curr->children[c];
        }

        curr->word = true;
    }

    bool isWord(string &word)
    {
        TrieNode* curr = root;

        for (auto c : word)
        {
            if (curr->children.find(c) == curr->children.end())
                return false;

            curr = curr->children[c];
        }

        return curr->word;
    }

    void getWord(TrieNode* root, vector<string> &words, string &word)
    {
        if (root->word)
        {
            words.push_back(word);
            
        }

        for (auto child : root->children)
        {
            word.push_back(child.first);
            getWord(child.second, words, word);
            word.pop_back();
        }
    }

    vector<string> getWords(string &prefix)
    {
        string word = "";
        TrieNode* curr = root;
        for (auto c : prefix)
        {
            if (curr->children.find(c) == curr->children.end())
                return {""};
            
            curr = curr->children[c];
        }

        vector<string> out;
        word = prefix;
        getWord(curr, out, word);
        return out;
    }

    bool isPrefix(string &prefix)
    {
        TrieNode* curr = root;

        for (auto c : prefix)
        {
            if (curr->children.find(c) == curr->children.end())
            {
                return false;
            }

            curr = curr->children[c];
        }

        return true;
    }
};

class Solution {
public:

    void print(vector<string> &words)
    {
        cout << "[ ";

        for (auto word : words)
        {
            cout << word << ", ";
        }

        cout << " ]" << endl;
    }

    bool wordBreak(string s, vector<string>& wordDict) 
    {
        unordered_set<string> dict = unordered_set(wordDict.begin(), wordDict.end());
        Trie trie;

        for (auto word : wordDict)
        {
            trie.addWord(word);
        }


        // string prefix = s.substr(0, 1);
        // vector<string> prefix_words = trie.getWords(prefix);
        // print(prefix_words);
        unordered_map<string, int> memo;

        for (auto dictWord : wordDict)
        {
            memo[dictWord] = 1;
        }

        bool ret = dp(s, memo, trie);
        return ret;
    }

    bool dp(string &word, unordered_map<string,int> &memo, Trie &trie)
    {
        if (word.length() == 0)
            return true;

        if (memo.find(word) != memo.end())
        {
            return bool(memo[word]);
        }
        bool ret = false;
        for (int j = 0 ; j < word.length() ; j++)
        {
            string prefix = word.substr(0, j+1);
            string suffix = word.substr(j+1, word.length());
            //cout << prefix << " : " << suffix << endl;
            if (trie.isWord(prefix))
            {
                memo[suffix] = int(dp(suffix, memo, trie));
                ret |= bool(memo[suffix]);
            }
        }

        return ret;
    }
};
26699