← 返回 reddit 的题目列表Shortest Palindrome
类型:qbank
Given a string, prepend the fewest characters to the front to make it a palindrome and return the result. This is LeetCode 214: the linear-time approach finds the longest palindromic prefix via a KMP failure function (or rolling hash) over s + separator + reverse(s), then mirrors the remaining suffix onto the front, handling inputs up to 5 * 10^4 lowercase letters.
Shortest Palindrome
Given a string, prepend the fewest characters to the front to make it a palindrome and return the result. This is LeetCode 214: the linear-time approach finds the longest palindromic prefix via a KMP failure function (or rolling hash) over s + separator + reverse(s), then mirrors the remaining suffix onto the front, handling inputs up to 5 * 10^4 lowercase letters.
MLE
SWE
string
palindromes
two-pointer
hard
Frequency
Single report
Last asked
2026-01-02
Stage
onsite-coding
Shortest Palindrome
You are given a string s. You may add characters only to the front of the string.
Return the shortest palindrome you can build this way.
Examples
Example 1:
Input: s = "aacecaaa"
Output: aaacecaaa
Example 2:
Input: s = "abcd"
Output: dcbabcd
Constraints
0 <= s.length <= 5 * 10^4
s consists of lowercase English letters.