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



class Solution {
public:
    unordered_map<int, bool> dp;
    bool wordhelper(string s, int ind,  unordered_map<string, bool>& worldmap) {
        if(s == "") {
            return true;
        }
        if(dp.count(ind)) {
            return dp[ind];
        }
        int size = s.size();
        string curr = "";
        for(int i = 0; i < size; i ++) {
            curr = curr + s[i];
            string rest = s.substr(i+1, size - i);
            if(worldmap[curr]) {

            bool ans = false;
            if(wordhelper(rest, ind + i, worldmap)) {
                dp[ind + i] = true;
                dp[ind] = true;
                return true;
             } else {
                dp[ind + i] = false;
             }
            }
        }
        dp[ind] = false;
        return false;
    }
        // if(ind >= s.size()) {
        //     if(curr == "") {
        //         //cout << "BLANK" << endl;
        //         return true;
        //     } else {
        //         return false;
        //     }
        // }
        // bool ans = false;
        // if(dp[curr + s[ind] + to_string(ind + 1)] == true || wordhelper(s, worldmap, ind + 1, curr + s[ind]) == true) {
        //    // cout << curr + s[ind] << endl;
        //     dp[curr + s[ind] + to_string(ind + 1)] = true;
        //     ans = true;
        // } else {
        //     dp[curr + s[ind] + to_string(ind + 1)] = false;
        // }
        // if(worldmap[curr + s[ind]] == true) {
        //  if(dp["" + to_string(ind + 1)] == true || wordhelper(s, worldmap, ind + 1, "") == true) {
        //     //cout << "BLANK" << ind << endl;
        //     dp["" + to_string(ind + 1)] = true;
        //     ans = true;
        //  } else {
        //     dp["" + to_string(ind + 1)] = false; 
        //  }
        // }
        // return ans;
    // }
    // bool askhay(int index, string s, unorderd_map<string>wordDict){
        
    //     for(int i=index:i<s.length();i++){
            
    //           string sub = s.substring(index,j-i+1)
    //            wordDict.find(){

    //           }
    //     }
    // }

    // int danishBhai(int index, string s, )
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_map<string, bool> worldmap;
        int size = wordDict.size();
        for(int i = 0; i < size; i++) {
            worldmap[wordDict[i]] = true;
        }
        worldmap[""] = true;
        return wordhelper(s,0, worldmap);
    }
};
27385