<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<int>> pal;
    int dp[2001][4];  // We only need up to 3 partitions (0 to 3), hence 4 columns

    bool checkPartitioning(string s) {
        int n = s.size();
        pal.resize(n, vector<int>(n, 0));

        // Step 1: Precompute the palindrome table
        for (int i = 0; i < n; i++) pal[i][i] = 1;

        for (int i = 0; i < n - 1; i++) {
            if (s[i] == s[i + 1]) pal[i][i + 1] = 1;
        }

        for (int k = 3; k <= n; k++) {
            for (int i = 0; i <= n - k; i++) {
                int j = i + k - 1;
                if (pal[i + 1][j - 1] && s[i] == s[j]) pal[i][j] = 1;
            }
        }

        
        memset(dp, 0, sizeof(dp));

        for (int i = 0; i < n; i++) {
            if (pal[0][i]) dp[i][1] = 1; 
        }

        
        for (int ct = 2; ct <= 3; ct++) {  
            for (int i = 0; i < n; i++) {
                for (int k = 0; k < i; k++) {
                    if (pal[k + 1][i] && dp[k][ct - 1]) {
                        dp[i][ct] = 1; 
                    }
                }
            }
        }
        return dp[n - 1][3];
    }
};

116493