-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathanswer.py
More file actions
25 lines (20 loc) · 767 Bytes
/
answer.py
File metadata and controls
25 lines (20 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
result = ""
for i in range(len(s)):
# Takes in account odd and even cases
result = max(result, self.find_palindrome(s, i, i), self.find_palindrome(s, i, i+1), key=len)
return result
# Helper method to find the longest palindrome starting from the center
def find_palindrome(self, s, left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left+1:right]
#-------------------------------------------------------------------------------
#Testing