-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.longest-palindromic-substring.py
53 lines (48 loc) · 1.38 KB
/
5.longest-palindromic-substring.py
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
#
# @lc app=leetcode id=5 lang=python3
#
# [5] Longest Palindromic Substring
#
# @lc code=start
from collections import deque
from inspect import stack
class Solution:
def longestPalindrome(self, s: str) -> str:
def maxPal(s):
if len(s) == 1 or s == s[::-1]:
return s
front = maxPal(s[1:])
end = maxPal(s[:-1])
return front if len(front) > len(end) else end
return maxPal(s)
def longestPalindrome2(self, s: str) -> str:
if not s:
return 0
stack = deque([s])
while stack:
node = stack.popleft()
if len(node) == 1:
continue
if node == node[::-1]:
return node
stack.append(node[1:])
stack.append(node[:-1])
return s[0]
def helper(self, s, l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l+1: r]
def longestPalindrome3(self, s: str) -> str:
res = ""
for i in range(len(s)):
for tmp in self.helper(s, i, i), self.helper(s, i, i+1):
if len(tmp) > len(res):
res = tmp
return res
# @lc code=end
import time
time1 = time.time()
n = "babaddtattarrattatddetartrateedredividerb"
pro = Solution()
print(pro.longestPalindrome3(n))