<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:
    void preprocessPalindrome(const string& s, vector<vector<bool>>& dp) {
        int n = s.size();
        for (int i = 0; i < n; ++i) {
            dp[i][i] = true;  // Single character substrings are palindromes
        }
        
        for (int i = 0; i < n - 1; ++i) {
            dp[i][i + 1] = (s[i] == s[i + 1]);  // Two-character substrings
        }
        
        for (int length = 3; length <= n; ++length) {
            for (int i = 0; i <= n - length; ++i) {
                int j = i + length - 1;
                dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1];
            }
        }
    }

    

    // bool isPal(string s, int st, int rt){
    //     while(st<=rt){
    //         if(s[st++]!=s[rt--])    return false;
    //     }
    //     return true;
    // }

    bool solve(string s, int k, int start, int end, vector<vector<vector<int>>> &dp, vector<vector<bool>> &pal){
        if(k<0) return false;
        if(start>end ){
            return k==0;
        }
        
        
        
        if(dp[start][end][k]!=-1)   return dp[start][end][k];
        for(int i=start;i<=end;i++){
            if(pal[start][i]){
                if(solve(s,k-1,i+1,end,dp,pal)){
                    return dp[start][end][k]=true;
                }
            }
            
        }
        return dp[start][end][k]=false;

    }

    bool checkPartitioning(string s) {
        
        int n = s.size();
        vector<vector<bool>> pal(n, vector<bool>(n, false));
        // vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(4,-1)));
        preprocessPalindrome(s, pal);
        // return solve(s,3,0,n-1,dp,pal);
        vector<bool> dp(n, false);
        vector<bool> new_dp(n, false);
        
        for (int k = 1; k <= 3; ++k) {
            fill(new_dp.begin(), new_dp.end(), false);
            
            for (int end = k - 1; end < n; ++end) {
                for (int start = k - 1; start <= end; ++start) {
                    if (pal[start][end] && (start == 0 || dp[start - 1])) {
                        new_dp[end] = true;
                        break;  // No need to check further for this `end`
                    }
                }
            }
            
            dp = new_dp;
        }
        
        return dp[n - 1];
    }
};
21866