<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, string: str) -> str:
            dictStr = {}
            for indexStr in range (len(string)):
                listSeen = dictStr.keys()
                #if already has this char in list, append index, next index
                if string[indexStr] in listSeen:
                    listTemp = dictStr[string[indexStr]]
                    listTemp.append(indexStr)
                else: #not in seen, create new key, add index
                    dictStr[string[indexStr]] = [indexStr]
                indexStr +=1
                
            #done create dict of letter, start calculating root
            dictRoot ={}
            for key in dictStr.keys():
                listTempKey = dictStr[key]
                for i in range (len(listTempKey)):
                    for j in range (i+1, len(listTempKey)):
                        if (listTempKey[j] + listTempKey[i])/2 not in dictRoot.keys():
                            dictRoot[(listTempKey[j] + listTempKey[i])/2] = [listTempKey[i]]
                        else:
                            listTemp = dictRoot[(listTempKey[j] + listTempKey[i])/2]
                            listTemp.append(listTempKey[i])
            
            #done calculating roots, start eliminate:
            maxCur = 0
            for root in dictRoot.keys():
                listTemp = dictRoot[root]
                listTemp.sort(reverse = True)
                for i in range (len(listTemp)):
                    if listTemp[i] + i + 1 != int(root + 0.5):
                        i = i-1
                        break
                if int(root)<root:
                    if maxCur < 2*i+2:
                        maxCur = 2*i+2
                        rootCur = root
                        
                else:
                    if maxCur < 2*i + 3:
                        maxCur = 2*i +3
                        rootCur = root
            
            if maxCur == 0:
                return string[0]
            else:
                if maxCur%1==0:
                    return string[int(rootCur - (maxCur-1)/2):int(rootCur + (maxCur+1)/2)]
                else:
                    return string[int(rootCur+0.5-maxCur/2):int(rootCur+0.5+maxCur/2)]
            


            
            



            
            
21700