-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path005-longest-palindromic-substring.py
More file actions
59 lines (55 loc) · 1.89 KB
/
005-longest-palindromic-substring.py
File metadata and controls
59 lines (55 loc) · 1.89 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution_slow:
def longestPalindrome(self, s: str) -> str:
def isPalindrome(string):
return string == string[::-1]
i = 0
res = ''
while i < len(s):
j = i
while j < len(s) + 1:
if isPalindrome(s[i:j]) and len(s[i:j]) > len(res):
res = s[i:j]
j += 1
i += 1
return res
class Solution_exp:
def longestPalindrome(self, s: str) -> str:
res = ''
for i in range(len(s)):
# the following part is for the odd length
j = i
k = i
while j >= 0 and k < len(s) and s[j]==s[k]:
if s[j] == s[k] and len(s[j:k+1]) > len(res):
print(1, s[j:k+1])
res = s[j:k+1]
j -= 1
k += 1
# this part is for even length
j = i
k = i + 1
while j >= 0 and k < len(s) and s[j]==s[k]: # you need to add this "s[j]==s[k]" to ensure all the loop are correct, otherwise, e.g. "aacabdkacaa"
if s[j] == s[k] and len(s[j:k+1]) > len(res):
print(2, s[j:k+1])
res = s[j:k+1]
j -= 1
k += 1
return res
class Solution:
def longestPalindrome(self, s: str) -> str:
res = ''
for i in range(len(s)):
palin1 = self.find_palindrome(s, i, i)
if len(palin1) > len(res):
res = palin1
palin2 = self.find_palindrome(s, i, i + 1)
if len(palin2) > len(res):
res = palin2
return res
def find_palindrome(self, s, left, right):
res = ''
while right < len(s) and left > -1 and s[left] == s[right]:
res = s[left:right + 1]
right += 1
left -= 1
return res