<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>

#define v vector<int>
#define vv vector<v>
#define pb push_back
#define vb vector<bool>
#define vvb vector<vb>

class Solution {
public:
    void print(v &arr) {
        for (int x : arr) {
            cout << x << " ";
        }
        cout << "\n";
    }

    void populatePalindromeLengthSingle(string &s, vv &palLength) {
        int n = s.length();
        for (int i=0; i<n; i++) {
            int l = i, r = i;
            while (l>=0 && r<n && s[l]==s[r]) {
                palLength[l].pb(r-l+1);
                l--;
                r++;
            }
        }
    }

    void populatePalindromeLengthDouble(string &s, vv &palLength) {
        int n = s.length();
        for (int i=0; i<n-1; i++) {
            int l = i, r = i+1;
            while (l>=0 && r<n && s[l]==s[r]) {
                palLength[l].pb(r-l+1);
                l--;
                r++;
            }
        }
    }

    int checkPartitionPossible(vv &palLength) {
        int n = palLength.size();
        vvb dp(n+1, vb(3, false));

        for (int i=n-1; i>=0; i--) {
            for (int len : palLength[i]) {
                // 1 parition
                dp[i][0] = dp[i][0] || (i+len == n);

                // 2 partitions
                dp[i][1] = dp[i][1] || dp[i+len][0];

                // 3 partitions
                dp[i][2] = dp[i][2] || dp[i+len][1];
            }
        }
        return dp[0][2];
    }
    
    bool checkPartitioning(string s) {
        int n = s.length();

        vv palindromeLength(n);
        populatePalindromeLengthSingle(s, palindromeLength);
        populatePalindromeLengthDouble(s, palindromeLength);

        return checkPartitionPossible(palindromeLength);
    }
};
62786