forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPalindrome.py
More file actions
57 lines (48 loc) · 1.43 KB
/
ShortestPalindrome.py
File metadata and controls
57 lines (48 loc) · 1.43 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
# -*- coding: utf-8 -*-
# @File : ShortestPalindrome.py
# @Date : 2020-02-20
# @Author : tc
"""
题号 214 最短回文串
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
示例 1:
输入: "aacecaaa"
输出: "aaacecaaa"
示例 2:
输入: "abcd"
输出: "dcbabcd"
代码参考:https://leetcode.com/problems/shortest-palindrome/solution/
思路参考:https://leetcode-cn.com/problems/shortest-palindrome/solution/c-li-yong-kmpsuan-fa-xiang-xi-tui-dao-zhuan-hua-gu/
"""
from typing import List
class Solution:
def shortestPalindrome(self, s: str) -> str:
n = len(s)
if not n:
return ''
rs = s[::-1]
i = 0
while True:
if rs[i:] == s[:n-i]:
break
i += 1
return rs[:i]+s
# KMP算法
def shortestPalindrome2(self, s: str) -> str:
n = len(s)
rev = s[::-1]
s_new = s + "#" + rev
n_new = len(s_new)
f = [0] * n_new
for i in range(1,n_new):
t = f[i - 1]
while t > 0 and s_new[i] != s_new[t]:
t = f[t - 1]
if s_new[i] == s_new[t]:
t += 1
f[i] = t
return rev[:n-f[n_new - 1]] + s
if __name__ == '__main__':
s = "aacecaaa"
solution = Solution()
print(solution.shortestPalindrome2(s))