<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 n = s.size(); 
        int i = 0, j = n-1; 

        while(i<j){
            if(s[i] != s[j]){
                return false;
            }
            i++; j--; 
        }
        return true;
    }

    bool rec(string &s, int k, int i, vector<vector<int>>& dp){
        int n = s.size();

        if(i>n-1){
            return false;
        }
        
        if(k<1){
            return false;
        }
        if(dp[i][k] != -1){
            return dp[i][k];
        }
        if(k==1){
            string temp; 
            for(int j = i; j<n; j++){
                temp.push_back(s[j]);
            }
            if(ispali(temp)){
                return dp[i][k] = true;
            }
            return dp[i][k] = false;
        }

        string temp ; 
        bool ans = false; 
        for(int end = i; end<n; end++){
            temp.push_back(s[end]); 
            //cout<<temp<<endl;
            // if(end == n-1 && k==1 && ispali(temp)){
            //     return true;
            // }
            if(ispali(temp)){
                //cout<<"HERE"<<temp<<ispali(temp)<<endl;
                ans = ans || rec(s, k-1, end+1, dp);
            }
        }
        //cout<<i<<" "<<k<<" "<<ans<<" "<<temp<<endl;
        return dp[i][k] = ans;
    }
    bool checkPartitioning(string s) {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(4, -1)); 
        return rec(s, 3, 0, dp);
    }
};
42326