forked from smettu1/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest-palindromic-substring
More file actions
31 lines (31 loc) · 1020 Bytes
/
Copy pathlongest-palindromic-substring
File metadata and controls
31 lines (31 loc) · 1020 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
//https://leetcode.com/problems/longest-palindromic-substring/submissions/
class Solution:
def longestPalindrome(self, s: str) -> str:
mlen = len(s)
mid = mlen//2
offset = 0
mstr = ''
plen = 0
if not s:
return ''
def ispal(str1):
mlen = len(str1)
if mlen%2 == 0:
if str1[0:mlen//2] == str1[mlen//2:][::-1]:
return True
else:
if str1[0:mlen//2] == str1[(mlen//2+1):][::-1]:
return True
return False
for i in range (mlen+1):
for j in range(mlen+1,0,-1):
if len(s[i:j]) > plen:
if ispal(s[i:j]):
print ('is palin ',s[i:j])
plen = len(s[i:j])
mstr = s[i:j]
if plen == mlen:
return mstr
if not mstr:
return s[0]
return mstr