<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 isPalindrome(int start,int end,string& s,vector<vector<int>>& dp)
    {
        if(start>=end)
        {
            return true;
        }

        if(s[start]!=s[end])
        {
            return false;
        }


        if(dp[start][end]!=-1)
        {
            return dp[start][end];
        }


        return dp[start][end]=isPalindrome(start+1,end-1,s,dp);
    }

    bool Solve(int i,int n,int k,string& s,vector<vector<int>>& dp1,vector<vector<int>>& dp2)
    {
        if(i==n)
        {
            return k==0;
        }


        if(k<0)
        {
            return false;
        }

        if(dp1[i][k]!=-1)
        {
            return dp1[i][k];
        }

        // string temp;
        for(int j=i;j<n;j++)
        {
            // temp+=s[j];
            if(isPalindrome(i,j,s,dp2))
            {
                bool ans=Solve(j+1,n,k-1,s,dp1,dp2);
                if(ans)
                {
                    return true;
                }
            }
        }


        return dp1[i][k]=false;
    }
    bool checkPartitioning(string s) {
        int k=3;
        int n=s.size();

        vector<vector<int>>dp1(n+1,vector<int>(4,-1));
        vector<vector<int>>dp2(n+1,vector<int>(n+1,-1));
        return Solve(0,n,k,s,dp1,dp2);
    }
};
129281