-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0005-longest-palindromic-substring.py
More file actions
79 lines (71 loc) · 2.44 KB
/
0005-longest-palindromic-substring.py
File metadata and controls
79 lines (71 loc) · 2.44 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# time complexity: O(n)
# space complexity: O(n)
# Manacher's Algorithm
class Solution:
def longestPalindrome(self, s: str) -> str:
sPrime = "#" + "#".join(s) + "#"
n = len(sPrime)
palindromeRadii = [0] * n
center = radius = 0
for i in range(n):
mirror = 2 * center - i
if i < radius:
palindromeRadii[i] = min(radius - i, palindromeRadii[mirror])
while (
i + 1 + palindromeRadii[i] < n
and i - 1 - palindromeRadii[i] >= 0
and sPrime[i + 1 + palindromeRadii[i]]
== sPrime[i - 1 - palindromeRadii[i]]
):
palindromeRadii[i] += 1
if i + palindromeRadii[i] > radius:
center = i
radius = i + palindromeRadii[i]
maxLength = max(palindromeRadii)
centerIndex = palindromeRadii.index(maxLength)
startIndex = (centerIndex - maxLength) // 2
longestPalindrome = s[startIndex: startIndex + maxLength]
return longestPalindrome
# Brute Force
# time complexity: O(n^3)
# space complexity: O(n^3)
class Solution:
def longestPalindrome(self, s: str) -> str:
def check(i, j):
left = i
right = j - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
for length in range(len(s), 0, -1):
for start in range(len(s) - length + 1):
if check(start, start + length):
return s[start: start + length]
return ""
# time complexity: O(n^2)
# space complexity: O(n^2)
# Dynamic Programming
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
dp = [[False for _ in range(n)] for _ in range(n)]
if n < 1:
return s
maxLen = 1
maxStr = s[0]
for right in range(n):
dp[right][right] = True
for left in range(right):
if s[left] == s[right] and (right - left <= 2 or dp[left + 1][right - 1]):
dp[left][right] = True
if right - left + 1 > maxLen:
maxLen = right - left + 1
maxStr = s[left:right + 1]
return maxStr
s = "babad"
print(Solution().longestPalindrome(s))
s = "cbbd"
print(Solution().longestPalindrome(s))