<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 {
    int dp[2005][4];
    bool ispalindrome(string &s){
        int n=s.size();
        int start=0,end=n-1;
        while(start<end){
            if(s[start]!=s[end])return false;
            start++;
            end--;
        }
        return true;
    }
    bool solve(int i,int k,string &s,int n,vector<vector<int>>&pre){
        if(i==n&&k==0)return true;
        if(dp[i][k]!=-1)return dp[i][k];
        int ans=false;
     
        for(int j=i;j<n;j++){
           
            if(pre[i][j]==1&&k>0){
                ans=ans|solve(j+1,k-1,s,n,pre);
            }
        }
        return dp[i][k]= ans;
    }
public:
    bool checkPartitioning(string s) {
         int n=s.size();
         vector<vector<int>>pre(n,vector<int>(n,0));
        memset(dp,-1,sizeof dp);
      for(int i=0;i<n;i++){
         int start=i;
         int end=i;
         while(start>=0&&end<n){
            if(s[start]==s[end]){
                pre[start][end]=1;
            }
            else break;

            start--,end++;
         }
      }
       for(int i=0;i<n;i++){
         int start=i;
         int end=i+1;
         while(start>=0&&end<n){
            if(s[start]==s[end]){
                pre[start][end]=1;
            }
            else break;
            start--,end++;
         }
      }
        return solve(0,3,s,n,pre);
    }
};
113936