-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
28 lines (26 loc) · 735 Bytes
/
5.py
File metadata and controls
28 lines (26 loc) · 735 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
26
27
28
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(s)==1:
return s
r = ['']
for j in range(len(s)):
r.append(s[j])
r.append('')
left = 0
right = 0
step = 1
for i in range(len(r)):
while (i - step) >= 0 and (i + step) < len(r):
if r[i - step:i + step + 1] == r[i + step:i - step-1:-1]:
left = i - step
right = i + step
step += 1
else:
break
return ''.join(r[left:right + 1])
solu = Solution()
print solu.longestPalindrome('ccc')