-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.py
More file actions
27 lines (24 loc) · 745 Bytes
/
Q5.py
File metadata and controls
27 lines (24 loc) · 745 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
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
size = len(s)
if(size<2):
return s
dp = [[False for i in range(size)] for i in range(size)]
res = s[0]
longest_s = 1
for r in range(1,size):
for l in range(r):
if s[l]==s[r] and (r-l<=2 or dp[l+1][r-1]==True):
dp[l][r] = True
cur_len = r-l+1
if cur_len>longest_s:
longest_s = cur_len
res = s[l:r+1]
return res
if __name__ == '__main__':
s = "aaaa"
Solution().longestPalindrome(s)