<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;babad&quot;
<strong>Output:</strong> &quot;bab&quot;
<strong>Explanation:</strong> &quot;aba&quot; is also a valid answer.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cbbd&quot;
<strong>Output:</strong> &quot;bb&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consist of only digits and English letters.</li>
</ul>

class Solution:

    def longestPalindrome(self, s: str) -> str:
        odd_palindromes = [[i for i in range(len(s))]]
        even_palindromes = [[i for i in range(len(s)-1) if s[i] == s[i+1]]]

        best_odd_size = 0
        best_even_size = -1 if len(even_palindromes[0]) == 0 else 0
        odd_size = 1
        even_size = 1

        while odd_size < len(s) and len(odd_palindromes[-1]) > 0:
            odd_palindromes.append([])

            for i in odd_palindromes[-2]:
                if i - odd_size >= 0 and i + odd_size < len(s) and s[i-odd_size] == s[i+odd_size]:
                    odd_palindromes[-1].append(i)
                    best_odd_size = odd_size

            odd_size += 1
        
        while even_size < len(s) and len(even_palindromes[-1]) > 0:
            even_palindromes.append([])

            for i in even_palindromes[-2]:
                if i - even_size >= 0 and i + even_size + 1 < len(s) and s[i-even_size] == s[i+even_size+1]:
                    even_palindromes[-1].append(i)
                    best_even_size = even_size
        
            even_size += 1

        if best_odd_size > best_even_size:
            odd_wins = True
            best_size = best_odd_size
        else:
            odd_wins = False
            best_size = best_even_size

        if odd_wins:
            if len(odd_palindromes[-1]) > 0:
                center = odd_palindromes[-1][0]
            else:
                center = odd_palindromes[-2][0]
        else:
            if len(even_palindromes[-1]) > 0:
                center = even_palindromes[-1][0]
            else:
                center = even_palindromes[-2][0]

        result = ''
        
        for i in range(center-best_size, center+best_size+1):
            result += s[i]
        
        if not odd_wins:
            result += s[center+best_size+1]

        return result
21100