<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:
    int palindrome(int i,int j,string &s,vector<vector<int>>&dp){
        // cout <<"i "<<i << " " << j << endl;
        //cbbbcaacb
        if(i>j) return dp[i][j] = 1;
        if(i==j) return dp[i][j]=1;
        if(dp[i][j]!=-1) return dp[i][j];
        int ans=0;
        palindrome(i,j-1,s,dp);
        palindrome(i+1,j,s,dp);
        if(palindrome(i+1,j-1,s,dp) && s[i]==s[j]) return  dp[i][j]=1;
        return dp[i][j]=0;
    }
    bool help(int i,int k,string &s,vector<vector<int>>&dp1 ,vector<vector<int>>&dp2){
        if(i>=s.size()) return (k==0);
        if(k==0) return false;
        if(dp2[i][k]!=-1) return dp2[i][k];
        int ans=false;
        for(int j=i;j<s.size();j++){
            if(dp1[i][j]==1){
                ans|=help(j+1,k-1,s,dp1,dp2);
            }
        }
        return dp2[i][k]=ans;
    }
    bool checkPartitioning(string s) {
        int n=s.size();
        vector<vector<int>> dp1(n+10,vector<int>(n+10,-1)),dp2(n+1,vector<int>(5,-1));
        palindrome(0,n-1,s,dp1);
        // for(int i=0;i<n;i++) dp1[i][i]=1;
        // for(int i = 0; i < n; i++) {
        //     for(int j = 0; j < n; j++) {
        //         cout << dp1[i][j] <<" ";
        //     }
        //     cout << endl;
        // }
        return  help(0,3,s,dp1,dp2);
    }
};
142068