-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
30 lines (29 loc) · 917 Bytes
/
5.py
File metadata and controls
30 lines (29 loc) · 917 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
#对于字符串str,假设dp[i,j]=1表示str[i...j]是回文子串
class Solution:
def longestPalindrome(self, s: str) -> str:
if not s: return ""
len_s = len(s)
if len_s==1:return s
longest = 1
start = 0
dp = [[0 for i in range(len_s)] for j in range(len_s)]
for i in range(len_s):
dp[i][i]=1
if i<len_s-1:
if s[i]==s[i+1]:
dp[i][i+1]=1
start = i
longest = 2
i=0
for l in range(3,len_s+1):
for i in range(0,len_s-l+1):
j = i+l-1
if(s[i]==s[j] and dp[i+1][j-1]==1):
dp[i][j]=1
start = i
longest=l
return s[start:start+longest]
if __name__ == '__main__':
ss = Solution()
s = "aaafasdfaasdsa"
ss.longestPalindrome(s)