<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:
    vector<vector<char>> dp;
    char isPalindrome(const string& s,int start,int end){
        if(dp[start][end] != -1) return dp[start][end];
        if(start>end) return 0;
        char ret = 1;
        while(start<end){
            if(s[start] != s[end]){
                ret = 0;
                break;
            }
            start++;
            end--;
        }
        dp[start][end] = ret;
        return ret;
    }
    bool checkPartitioning(string s) {
        int mid1 = 0;
        int N = s.length();
        dp.resize(N,vector<char>(N,-1));
        for(;mid1<N/2;++mid1){
            if(mid1*2 >= N-2){
                break;
            }
            if(isPalindrome(s,0,mid1*2)){
                int start = mid1*2+1;
                int mid2 = start;
                for(;mid2 < N;++mid2){
                    int end = start+(mid2-start)*2;
                    if(end >= N-1){
                        break;
                    }
                    if(isPalindrome(s,start,end) && isPalindrome(s,end+1,N-1)) {
                        return true;
                    }
                    else if(isPalindrome(s,start,end+1) && isPalindrome(s,end+2,N-1)){
                        return true;
                    }
                }
            }
            if(isPalindrome(s,0,mid1*2+1)){
                int start = mid1*2+2;
                int mid2 = start;
                for(;mid2 < N;++mid2){
                    int end = start+(mid2-start)*2;
                    if(end >= N-1){
                        break;
                    }
                    if(isPalindrome(s,start,end) && isPalindrome(s,end+1,N-1)) {
                        return true;
                    }
                    else if(isPalindrome(s,start,end+1) && isPalindrome(s,end+2,N-1)){
                        return true;
                    }
                }
            }
        }
        return false;
    }
};
55113