-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path214 Shortest Palindrome.py
67 lines (53 loc) · 1.37 KB
/
214 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
"""
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".
Author: Rajeev Ranjan
"""
class Solution:
def shortestPalindrome(self, s):
"""
KMP
:type s: str
:rtype: str
"""
s_r = s[::-1]
l = len(s)
if l < 2:
return s
# construct T
T = [0 for _ in xrange(l+1)]
T[0] = -1
pos = 2
cnd = 0
while pos <= l:
if s[pos-1] == s[cnd]:
T[pos] = cnd+1
cnd += 1
pos += 1
elif T[cnd] != -1:
cnd = T[cnd]
else:
T[pos] = 0
cnd = 0
pos += 1
# search
i = 0
b = 0
while i+b < l:
if s[i] == s_r[i+b]:
i += 1
if i == l:
return s
elif T[i] != -1:
b = b+i-T[i]
i = T[i]
else:
b += 1
i = 0
# where it falls off
return s_r+s[i:]
if __name__ == "__main__":
assert Solution().shortestPalindrome("abcd") == "dcbabcd"