Python 3 | Longest Palindromic Substring | not DP but meet time limitation


#1

class Solution:
# @param A : string
# @return a strings
def longestPalindrome(self, A):
length = 1
ret = A[0]
for i in range(len(A)-1):
if len(A)-i<length:
break
for j in range(len(A)-1,i,-1):
if j+1-i<length:
break
if A[i:j+1] == A[i:j+1][::-1]:
if len(A[i:j+1]) > length:
length = len(A[i:j+1])
ret = A[i:j+1]

    return ret