-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0005.py
More file actions
35 lines (29 loc) · 913 Bytes
/
q0005.py
File metadata and controls
35 lines (29 loc) · 913 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
29
30
31
32
33
34
35
#!/usr/bin/python3
from typing import List
class Solution:
def longestPalindrome(self, s: str) -> str:
length = len(s)
dp = [[False for _ in range(length)] for _ in range(length)]
chars = [s[i] for i in range(length)]
index_i = 0
index_j = 0
for i in range(length):
dp[i][i] = True
i = length - 2
while i >= 0:
j = i + 1
while j < length:
if chars[i] == chars[j]:
if j - i == 1:
dp[i][j] = True
else:
dp[i][j] = dp[i + 1][j - 1]
if dp[i][j] and j - i > index_j - index_i:
index_j = j
index_i = i
j += 1
i -= 1
return s[index_i: index_j + 1]
s = "babad"
result = Solution().longestPalindrome(s)
print(result)