<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:
        ilen = len(s)
        if ilen == 1:
            return s
        elif ilen == 2:
            if s[0]==s[1]:
                return s
            else:
                return s[0]
        kw = {}
        for i in range(ilen-1):
            for j in range(i+2, min(i+5, ilen+1)):
                a = s[i:j]
                if a == a[::-1]:
                    k = 1
                    while True:
                        ni = i-k
                        nj = j+k -1
                        if ni >= 0 and nj < ilen and s[ni] == s[nj]:
                             k+=1
                        else:
                            break
                    ni = i-(k-1)
                    nj = j+(k-1)
                    kw[(ni, nj)] = s[ni:nj] 
                             

        # for i in range(ilen-1):
        #    for j in range(i+1, ilen):
        #        if (i, j) not in kw:
        #            a = s[i:j+1]
        #            if a == a[::-1]:
        #                kw[(i,j)] = a
        if kw:
            # print(kw)
            vs = list(kw.values())
            
            vs.sort(key= lambda a: len(a))
            return vs[-1]
            # return s[0]
        else:
            return s[0]


        
18400