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

        # find cores of palindromes:
        n = len(s)
        cores = []
        for i in range(n-1):
            if s[i] == s[i+1]:  # for types 'aa'
                cores.append((i, i+1))
            if i+2 < n and s[i] == s[i+2]:  # for types 'aba'
                cores.append((i, i+2))
        
        # find boundaries of palindromes using its cores:
        heap = []
        for core in cores:
            l, r = core
            while l > 0 and r < n-1:
                if s[l-1] == s[r+1]:
                    l -= 1
                    r += 1
                else:
                    break
            heappush(heap, (-r+l-1, l, r))

        # for cases where max length of palindrome is 1:
        heappush(heap, (-1, 0, 0))

        # get palindrome:
        num, l, r = heappop(heap)
        return s[l:r+1]
        

            


        
17200