<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:
    bool exists(string& s, int i, int len, unordered_set<string>& lookUp,vector<vector<int>>& dp){
        //std::cout<<i<<"\t"<<len<<std::endl;
        if(len == 0 )return true;
        if(i < 0) return false;

        if(dp[i][len] != -1) return dp[i][len];
        string temp = s.substr(i ,len-i);
        //std::cout<<temp<<std::endl;
        if(lookUp.count(temp)){
            return dp[i][len] = (exists(s,i-1, len-temp.size(),lookUp,dp) || exists(s,i-1,len,lookUp,dp));
        }
        else{
            return dp[i][len] = exists(s,i-1,len,lookUp,dp);
        }
    }
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> lookUp(wordDict.begin(),wordDict.end());
        // vector<vector<int>> dp(s.size(),vector<int>(s.size()+1,-1));
        // return exists(s,s.size()-1,s.size(),lookUp,dp);
        //bottom up

        // vector<vector<bool>> dp(s.size()+1,vector<bool>(s.size()+1,false));
        // for(int i = 0;i<s.size()+1;i++){
        //     dp[0][i] = true;
        // }

        // for(int i = 1;i<s.size()+1;i++){
        //     for(int j = 1;j<=i;j++){
        //         string temp = s.substr(j-1,i-j+1);
        //         //std::cout<<temp<<std::endl;
        //         if(lookUp.count(temp)){
        //             dp[i][j] = dp[j][j-1] || dp[i][j-1];
        //         }
        //         else{
        //             dp[i][j] = dp[i][j-1];
        //         }
        //     }
        // }

        // for(auto& x: dp){
        //     for(auto y: x)
        //         std::cout<<y<<"\t";
        //     std::cout<<std::endl;
        // }
        //return dp[s.size()][s.size()];
        // 1D dp

        vector<bool> dp(s.size()+1,false);
        dp[0] = true;
        for(int i = 1;i<s.size()+1;i++){
            for(int j = 0;j<i;j++){
                if(dp[j] && lookUp.count(s.substr(j,i-j))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.size()];

    }
};
16176