Skip to content

Instantly share code, notes, and snippets.

@robbywalker
Created October 21, 2010 22:58
Show Gist options
  • Save robbywalker/639542 to your computer and use it in GitHub Desktop.
Save robbywalker/639542 to your computer and use it in GitHub Desktop.
O(n^2) solution to GPCv1 Level 1
def get_longest_palindrome(text):
best_palindrome = ''
length = len(text)
for i in range(length * 2 - 1):
start = i / 2 - i % 2
end = i / 2 + 1
while start > 0 and end < length and text[start] == text[end]:
start -= 1
end += 1
candidate = text[start+1:end]
if len(candidate) > len(best_palindrome):
best_palindrome = candidate
return best_palindrome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment