-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq5_longestPalindrome.py
40 lines (36 loc) · 1.08 KB
/
q5_longestPalindrome.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
__author__ = 'gaochenchao'
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return s
maxl, maxr, length = 0, 0, 0
n = len(s)
for i in xrange(n):
start = i
end = i + 1
while start >= 0 and end < n:
if s[start] == s[end]:
if end - start + 1 > length:
maxl, maxr, length = start, end, end - start + 1
start -= 1
end += 1
else:
break
start = i - 1
end = i + 1
while start >= 0 and end < n:
if s[start] == s[end]:
if end - start + 1 > length:
maxl, maxr, length = start, end, end - start + 1
start -= 1
end += 1
else:
break
return s[maxl:maxr+1]
st = Solution()
print st.longestPalindrome("aaaaa")