forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest-palindrome.py
82 lines (73 loc) · 2.12 KB
/
shortest-palindrome.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
71
72
73
74
75
76
77
78
79
80
81
82
# Time: O(n)
# Space: O(n)
#
# Given a string S, you are allowed to convert it to a palindrome
# by adding characters in front of it. Find and return the shortest
# palindrome you can find by performing this transformation.
#
# For example:
#
# Given "aacecaaa", return "aaacecaaa".
#
# Given "abcd", return "dcbabcd".
#
# KMP Algorithm
class Solution(object):
def shortestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
def getPrefix(pattern):
prefix = [-1] * len(pattern)
j = -1
for i in xrange(1, len(pattern)):
while j > -1 and pattern[j+1] != pattern[i]:
j = prefix[j]
if pattern[j+1] == pattern[i]:
j += 1
prefix[i] = j
return prefix
if not s:
return s
A = s + s[::-1]
prefix = getPrefix(A)
i = prefix[-1]
while i >= len(s):
i = prefix[i]
return s[i+1:][::-1] + s
# Time: O(n)
# Space: O(n)
# Manacher's Algorithm
class Solution2(object):
def shortestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
def preProcess(s):
if not s:
return ['^', '$']
string = ['^']
for c in s:
string += ['#', c]
string += ['#', '$']
return string
string = preProcess(s)
palindrome = [0] * len(string)
center, right = 0, 0
for i in xrange(1, len(string) - 1):
i_mirror = 2 * center - i
if right > i:
palindrome[i] = min(right - i, palindrome[i_mirror])
else:
palindrome[i] = 0
while string[i + 1 + palindrome[i]] == string[i - 1 - palindrome[i]]:
palindrome[i] += 1
if i + palindrome[i] > right:
center, right = i, i + palindrome[i]
max_len = 0
for i in xrange(1, len(string) - 1):
if i - palindrome[i] == 1:
max_len = palindrome[i]
return s[len(s)-1:max_len-1:-1] + s