<p>Given a string <code>s</code>, return <code>true</code> <em>if it is possible to split the string</em> <code>s</code> <em>into three <strong>non-empty</strong> palindromic substrings. Otherwise, return </em><code>false</code>.​​​​​</p>

<p>A string is said to be palindrome if it the same string when reversed.</p>

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

<pre>
<strong>Input:</strong> s = &quot;abcbdd&quot;
<strong>Output:</strong> true
<strong>Explanation: </strong>&quot;abcbdd&quot; = &quot;a&quot; + &quot;bcb&quot; + &quot;dd&quot;, and all three substrings are palindromes.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;bcbddxy&quot;
<strong>Output:</strong> false
<strong>Explanation: </strong>s cannot be split into 3 palindromes.
</pre>

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

<ul>
	<li><code>3 &lt;= s.length &lt;= 2000</code></li>
	<li><code>s</code>​​​​​​ consists only of lowercase English letters.</li>
</ul>

class Solution {
public:
    
    bool isPali(string& s, int i, int j, vector<vector<int>>& check)
    {
        //int n = s.size();
        //int i = 0, j = n-1;
        if(i>=j)
        return true;

        if(check[i][j]!=-1)
        return check[i][j];

        bool flag = true;
        if(s[i]==s[j])
        flag = flag & isPali(s, i+1, j-1, check);
        else
        return check[i][j] = false;
        if(flag)
        check[i][j]=1;
        else
        check[i][j]=0;
        return check[i][j];
    }
    // bool solve(string& s, int j, int k)
    // {
    //     int dp[2000][4];
    //     if(!k)
    //     {
    //         if(s.size()==j)
    //         return true;
    //         return false;
    //     }

    //     if(k<0)
    //     return false;

    //     if(dp[j][k]!=-1)
    //     return dp[j][k];
    //     bool flag = false;
    //     for(int i=j;i<s.size();i++)
    //     {
    //         //string t = s.substr(j, i-j+1);
    //         if(isPali(s, i, j))
    //         {
    //             //string y = s.substr(i+1);
    //             flag = flag | solve(s, i+1, k-1);
    //             if(flag)
    //             break;
    //         }
    //     }
    //     if(flag)
    //     return dp[j][k]=1;
    //     return dp[j][k]=0;
    // }
    bool checkPartitioning(string s) {
        // memset(dp, -1, sizeof(dp));
        // return solve(s, 0, 3);
        int n = s.size();
        vector<vector<bool>> dp(n+1, vector<bool>(4, false));
        vector<vector<int>> check(n+1, vector<int>(n+1, -1));
        dp[n][0]=true;
        for(int j=n-1;j>=0;j--)
        {
            for(int k=1;k<=3;k++)
            {
                bool flag = false;
                for(int i=j;i<s.size();i++)
                {
                    //string t = s.substr(j, i-j+1);
                    if(isPali(s, j, i, check))
                    {
                        //string y = s.substr(i+1);
                        flag = flag | dp[i+1][k-1];
                        if(flag)
                        break;
                    }
                }
                dp[j][k]=flag;
            }
        }
        return dp[0][3];
    }
};
139511