给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
示例 1:
输入: "aacecaaa"
输出: "aaacecaaa"
示例 2:
输入: "abcd"
输出: "dcbabcd"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shortest-palindrome
解法一:字符串逆序匹配,startswith(匹配字符串的开头endswith匹配末尾)
class Solution: def shortestPalindrome(self, s: str) -> str: if not s: return '' for i in range(len(s)): if s.startswith(s[::-1][i:]): return s[::-1][:i]+s
解法二:KMP算法,emmm,还没搞懂
class Solution:
def shortestPalindrome(self, s: str) -> str:
def get_table(p):
table = [0] * len(p)
i = 1
j = 0
while i < len(p):
if p[i] == p[j]:
j += 1
table[i] = j
i += 1
else:
if j > 0:
j = table[j - 1]
else:
i += 1
j = 0
return table
table = get_table(s + "#" + s[::-1])
return s[table[-1]:][::-1] + s
链接:https://leetcode-cn.com/problems/shortest-palindrome/solution/zui-duan-hui-wen-chuan-by-powcai/