<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:
    def checkPartitioning(self, s: str) -> bool:
        # we are just checking for three non-empty palindromic substrings
        # palindrome is when it is reversed

        # s size: [3, 2000]
        # s lowercase english letters

        # abcbdd
        # slightly inefficient solution, maybe O(N^2)
        # brute force: all possible substrings is O(N^3) (in other words three pointers)
        # how could we maybe push this down to two pointers?

        # first find all possible starting palindromes, and then use a two pointer method

        # 3D Dp: the indices for the dp table: (i, j, p) # i = start, j = end, p = # of palindromes
        # include the fact that end is not inclusive
        prefix = [False] * len(s)
        suffix = [False] * len(s)
        for i in range(len(s)):
            string = s[:i+1]
            mid = len(string) // 2
            if len(string) == 1 or (len(string) % 2 == 0 and string[:mid] == string[mid:][::-1]) or (len(string) % 2 == 1 and string[:mid] == string[mid+1:][::-1]):
                prefix[i] = True
        for i in range(len(s) -1, -1, -1):
            string = s[i:]
            mid = len(string) // 2
            if len(string) == 1 or (len(string) % 2 == 0 and string[:mid] == string[mid:][::-1]) or (len(string) % 2 == 1 and string[:mid] == string[mid+1:][::-1]):
                suffix[i] = True
        for start in range(1, len(s) - 1):
            for end in range(start, len(s) - 1):
                string = s[start:end+1] # [start, end]
                mid = len(string) // 2
                if len(string) == 1 or (len(string) % 2 == 0 and string[:mid] == string[mid:][::-1]) or (len(string) % 2 == 1 and string[:mid] == string[mid+1:][::-1]):
                    # string is a palindrome
                    if prefix[start-1] and suffix[end + 1]:
                        return True
        return False
        # dp = [[False] * 4 for _ in range(len(s))]
        # dp[i] = s[i-1]
        # we don't care about (start, end) bounds we just care about whether there is a palindrome count at that index
        
        # for i in range(len(s)): 
        #     string = s[:i+1]
        #     mid = len(string) // 2
        #     if len(string) == 1 or len(string) % 2 == 0 and string[:mid] == string[mid:] or len(string) % 2 == 1 and string[:mid] == string[mid+1:]:
        #         dp[i][1] = True
        # # if dp[i][1] is true, there is a palindrome up to including i
        # for end in range(len(s)):
        #     # we need to include the edge case of len(string) == 1 # come back to this
        #     for start in range(1, end + 1): # start is not going to be inclusive, i.e if our start is 1, that means we need to look at the one before start to get the values
        #         # we need to check the palindrome here
        #         string = s[start:end] # now we have the single letter case
        #         mid = len(string) // 2
        #         if len(string) == 1 or len(string) % 2 == 0 and string[:mid] == string[mid:] or len(string) % 2 == 1 and string[:mid] == string[mid+1:]: # palindrome
        #             # we need to look at start
        #             if len(string) >= 1:
        #                 for p in range(2, 4): # all palindromes (1, 2, 3)
        #                     dp[end][p] = dp[end][p] or dp[start - 1][p-1]
        # print(dp)
        # return dp[-1][3]


5015