Skip to content

Instantly share code, notes, and snippets.

@Faiz-zz-zz
Created July 14, 2016 15:36
Show Gist options
  • Save Faiz-zz-zz/8e0620f57bef713614f2bed7559df831 to your computer and use it in GitHub Desktop.
Save Faiz-zz-zz/8e0620f57bef713614f2bed7559df831 to your computer and use it in GitHub Desktop.
"""
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
"""
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
start = 0
end = len(s)
maxima = ""
for i in range(len(s)):
for j in reversed(range(len(s))):
if s[i] == s[j]:
start = i
end = j
length = end - start + 1
while(s[start] == s[end] and start != end and start < end):
start += 1
end -= 1
if start >= end:
maxima = max((maxima, s[i:j+1]), key = lambda k: len(k))
return maxima
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment