-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestPalindrome.py
70 lines (60 loc) · 1.64 KB
/
longestPalindrome.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# @return a string
# DP approach, O(n^2)
def longestPalindrome(s):
l = len(s)
maxStr = s[0]
maxLen = 1
p = [[0 for i in range(l)] for j in range(l)]
for i in range(l - 1):
p[i][i] = 1
if s[i] == s[i + 1]:
if maxLen < 2:
maxLen = 2
maxStr = s[i:(i + 2)]
p[i][i + 1] = 1
else:
p[i][i + 1] = 0
for k in range(2, l):
for i in range(0, l - k):
j = i + k
if p[i + 1][j - 1] and s[i] == s[j]:
p[i][j] = 1
if maxLen < (j - i + 1):
maxLen = j - i + 1
maxStr = s[i:(j + 1)]
else:
p[i][j] = 0
return maxStr
# O(n)
def longestPalindromeSubstring(s):
def preProcess(s):
if len(s) == 0:
return ''
ret = '^'
for c in s:
ret += '#' + c
return ret + '#$'
T = preProcess(s)
n = len(T)
C = 0
R = 0
P = [0 for i in range(n)]
for i in range(1, n - 1):
iMirror = C - (i - C)
P[i] = max(min(R - i, P[iMirror]), 0)
while T[i - P[i] - 1] == T[i + P[i] + 1]:
P[i] += 1
if P[i] + i > R:
R = P[i] + i
C = i
maxLen = 0
centerIndex = 0
for i in range(1, n - 1):
if P[i] > maxLen:
maxLen = P[i]
centerIndex = i
start = (centerIndex - 1 - maxLen) / 2
return s[start:(start + maxLen)];
assert longestPalindromeSubstring('helloworld') == 'owo'
assert longestPalindromeSubstring('abbc') == 'bb'
assert longestPalindromeSubstring('spring') == 's'