-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
48 lines (46 loc) · 1.17 KB
/
5.py
File metadata and controls
48 lines (46 loc) · 1.17 KB
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
41
42
43
44
45
46
47
48
class Solution(object):
# def longestPalindrome(self, s):
# """
# :type s: str
# :rtype: str
# """
# max = ''
# lens = len(s)
# for i in range(lens):
# for j in range(i+1,lens+1):
# # print j
# substr = s[i:j]
# if len(substr)>len(max):
# if self.isPalindrome(substr):
# max = substr
# return max
# def isPalindrome(self, l):
# lens = len(l)
# flag = True
# for i in range(lens/2):
# if l[i] != l[lens-i-1]:
# flag = False
# break
# return flag
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
lens = len(s)
max = ''
for i in range(lens):
substr = self.getlongestPalindrome(s, i,i)
if len(max)<len(substr):
max = substr
substr = self.getlongestPalindrome(s,i,i+1)
if len(max)<len(substr):
max = substr
return max
def getlongestPalindrome(self, s, l,r):
while l>=0 and r<len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l+1:r]
test = Solution()
print test.longestPalindrome('aaabbbbaaa')