-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
25 lines (19 loc) · 713 Bytes
/
5.py
File metadata and controls
25 lines (19 loc) · 713 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
class Solution:
def longestPalindrome(self, s: str) -> str:
if not s:
return ""
def expand_around_center(s: str, left: int, right: int):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return right - left - 1
start = 0
end = 0
for i in range(len(s)):
odd = expand_around_center(s, i, i)
even = expand_around_center(s, i, i + 1)
max_len = max(odd, even)
if max_len > end - start:
start = i - (max_len - 1) // 2
end = i + max_len // 2
return s[start:end+1]