Created
October 21, 2010 22:58
-
-
Save robbywalker/639542 to your computer and use it in GitHub Desktop.
O(n^2) solution to GPCv1 Level 1
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
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