<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 memo1[2001][2001];
    int memo2[2001][2001];
    bool pal(int i, int j, string& s) {
        if (i >= j) return true;
        if (memo1[i][j] != -1) return memo1[i][j];
        memo1[i][j] = 0;
        if (s[i] == s[j]) memo1[i][j] = pal(i + 1, j -1, s);
        pal(i + 1, j, s);
        pal(i, j - 1, s);
        return memo1[i][j];
    }

    bool rec(int i, int cnt, string& s) {
        if (i == s.size()) return cnt == 3;
        if (memo2[i][cnt] != -1) return memo2[i][cnt];
        bool ans = false;
        for (int j = i; !ans && j < s.size(); j++) {
            if (memo1[i][j]) ans = rec(j + 1, cnt + 1, s);
        }

        return memo2[i][cnt] = ans;
    }

    bool checkPartitioning(string s) {
        memset(memo1, -1, sizeof(memo1));
        // memset(memo2, -1, sizeof(memo2));
        pal(0, s.size() - 1, s);
        for (int i =1; i < s.size() - 1; i++) {
            for (int j = i; j < s.size() - 1; j++) {
                if (memo1[0][i - 1] && memo1[i][j] && memo1[j + 1][s.size() - 1]) return true;
            }
        }
        return false;
    }
};
37211