<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 dp[2001][4];
    
    // function to check for palindrome strings
    bool checkPalindrome(string &s)
    {
        int i=0;
        int j=s.size()-1;
        while(i<j)
        {
            if(s[i]!=s[j])return false;
            i++;
            j--;
        }
        return true;
    }
    int recur(int index,string &s,int partition,int n)
    {
        /* if we succesfully partitioned into 3 palindromic substrings and reached the end of string
         we simply return true 
       */
        if(partition==0 && index>=n)return 1;
        
        /* if we reach end of string and still partition left or partition over and string 
         remaining then we return false as its not possible for us to get correct result
        */
        if(partition==0 || index>=n)return 0;
        
        // initial ans==0 meaning not possible
        int  ans=0;
        
        /* if particular states have already found before we 
         return value from dp cache
        */
		if(dp[index][partition]!=-1)return dp[index][partition];
        
        // creating string till curr index to till partition index
       
        string res="";
        
        for(int partitionIndex=index;partitionIndex<s.size();partitionIndex++)
        {
            res+=s[partitionIndex];
            if(checkPalindrome(res)==true)
            {
                /* taking or operation as at point of time we get true meaning we have 
                 achived our result
                */
				ans|=recur(partitionIndex+1,s,partition-1,n);
            }
        }
        // storing in dp if it was not present in the cache
        return dp[index][partition]=ans;
    }
    bool checkPartitioning(string s) {
        // Initializaing dp vector 
        
        int n=s.size();
        
        memset(dp,-1,sizeof(dp));
         
        return recur(0,s,3,n);
    }
};
39768