Created
July 14, 2016 15:36
-
-
Save Faiz-zz-zz/8e0620f57bef713614f2bed7559df831 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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