# 5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Output: "bab"
Note: "aba" is also a valid answer.

Input: "cbbd"
Output: "bb"

class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
def is_palindromic(list):
return list == list[::-1], len(list), list

def find_next(list1, tar):
# print(list1)
list3 = []
list2 = list1[::-1]
# print(list2)
for i, each in enumerate(list2):
if each == tar:
list3.append(len(list1) - i)

return list3

max = 0
SUBlist = ""
if len(s) == 1:
return s
for i, each in enumerate(s):
same_char_list = find_next(s[i + 1:], each)
if same_char_list is not None and len(same_char_list) != 0:
for each in same_char_list:
# print(i, i + each)
flag, lenght, sublist = is_palindromic(s[i:1 + i + each])
if flag:
if max < lenght:
max = lenght
SUBlist = sublist
if lenght == len(s):
return SUBlist
else:
if SUBlist == "":
SUBlist = each
return SUBlist

solution = Solution()
print(solution.longestPalindrome("aaabaaaa"))