-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path91.py
30 lines (22 loc) · 749 Bytes
/
91.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
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
dp = [[False for _ in range(n)] for _ in range(n)]
for i in range(n):
dp[i][i] = True
start = 0
maxLength = 1
for i in range(n - 1):
if s[i] == s[i + 1]:
dp[i][i + 1] = True
start = i
maxLength = 2
for k in range(3, n + 1):
for i in range(0, n - k + 1):
j = i + k - 1
if dp[i + 1][j - 1] and s[i] == s[j]:
dp[i][j] = True
if k > maxLength:
maxLength = k
start = i
return s[start:start + maxLength]