[LeetCode] 214. Shortest Palindrome 最短回文串
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.
Example 1:
"aacecaaa"
"aaacecaaa"
Example 2:
"abcd"
"dcbabcd"
给一个字符串s,允许在它的前面加字符使其变成回文字符串,找出最短的回文字符串。
解法1: 暴力匹配算法brute force,无法通过OJ,需要用比较巧妙的方法来解。
解法2: KMP算法。
解法3: Manacher's算法。
参考:从头到尾彻底理解KMP
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Solution { public String shortestPalindrome(String s) { String r = new StringBuilder(s).reverse().toString(); String t = s + "#" + r; int [] next = new int [t.length()]; for ( int i = 1 ; i < t.length(); ++i) { int j = next[i - 1 ]; while (j > 0 && t.charAt(i) != t.charAt(j)) j = next[j - 1 ]; j += (t.charAt(i) == t.charAt(j)) ? 1 : 0 ; next[i] = j; } return r.substring( 0 , s.length() - next[t.length() - 1 ]) + s; } } |
Python: KMP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution( object ): def shortestPalindrome( self , s): 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 |
Python: Manacher's
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 | class Solution( object ): def shortestPalindrome( self , s): 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 |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Solution { public : string shortestPalindrome(string s) { string r = s; reverse(r.begin(), r.end()); string t = s + "#" + r; vector< int > next(t.size(), 0); for ( int i = 1; i < t.size(); ++i) { int j = next[i - 1]; while (j > 0 && t[i] != t[j]) j = next[j - 1]; next[i] = (j += t[i] == t[j]); } return r.substr(0, s.size() - next.back()) + s; } }; |
类似题目:
[LeetCode] 9. Palindrome Number 验证回文数字
[LeetCode] 5. Longest Palindromic Substring 最长回文子串
[LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
[LeetCode] 125. Valid Palindrome 验证回文字符串
[LeetCode] 409. Longest Palindrome
All LeetCode Questions List 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构