LeetCode Problem

How to Solve Shortest Palindrome

The problem requires transforming a string into its shortest palindrome by adding characters at the front. A rolling hash approach can be used to optimize this process. The solution efficiently finds the minimal characters to prepend, making use of string matching techniques and the pattern of finding the longest palindromic prefix.

GhostInterview Help

Need help with Shortest Palindrome without spending extra time grinding it?

GhostInterview can read Shortest Palindrome from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #214String plus Rolling HashReviewed 2026-03-08
Difficulty
Hard
Primary pattern
String plus Rolling Hash
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem requires transforming a string into its shortest palindrome by adding characters at the front. A rolling hash approach can be used to optimize this process. The solution efficiently finds the minimal characters to prepend, making use of string matching techniques and the pattern of finding the longest palindromic prefix.

Problem Statement

Given a string s, you need to transform it into a palindrome by adding characters only at the front. The goal is to perform this transformation with the fewest characters added, returning the shortest possible palindrome. The input string will only contain lowercase English letters.

For example, given s = "aacecaaa", the shortest palindrome formed by adding characters at the front is "aaacecaaa". Another example is s = "abcd", which results in "dcbabcd" when transformed into the shortest palindrome.

Examples

Example 1

Input: s = "aacecaaa"

Output: "aaacecaaa"

Example details omitted.

Example 2

Input: s = "abcd"

Output: "dcbabcd"

Example details omitted.

Constraints

  • 0 <= s.length <= 5 * 104
  • s consists of lowercase English letters only.

Solution Approach

Prefix Search with Rolling Hash

The solution involves using rolling hash to efficiently find the longest palindromic prefix in the string. By calculating the hash of the string and comparing it with the reverse, we can determine the largest palindrome that can be formed by appending characters from the string's end.

String Matching and KMP Algorithm

We utilize the Knuth-Morris-Pratt (KMP) algorithm to efficiently find the longest palindromic prefix by performing a string matching operation. This approach avoids redundant comparisons and speeds up the process, leading to an optimal solution.

Efficient Prepend of Characters

Once the longest palindromic prefix is identified, we can prepend the remaining part of the string (that does not form a palindrome) in reverse order to the beginning of the string. This step guarantees the formation of the shortest palindrome.

Complexity Analysis

MetricValue
TimeO(n)
SpaceO(n)

The time complexity of the solution is O(n), where n is the length of the string. This is because the rolling hash and KMP algorithm each process the string in linear time. The space complexity is also O(n), as we store hash values and the reversed string for comparison.

What Interviewers Usually Probe

  • Understanding of rolling hash and string matching techniques.
  • Ability to implement optimal algorithms for palindrome construction.
  • Knowledge of the KMP algorithm and its application to string problems.

Common Pitfalls or Variants

Common pitfalls

  • Overcomplicating the solution by brute-forcing palindrome checks for each substring.
  • Failing to optimize the solution, leading to an O(n^2) time complexity due to repeated comparisons.
  • Not handling edge cases like an empty string or already palindromic strings efficiently.

Follow-up variants

  • Shortest palindrome with case-sensitive characters.
  • Transforming the string to a palindrome by adding characters at both ends.
  • Finding the shortest palindrome by appending characters at the end instead of the front.

How GhostInterview Helps

  • GhostInterview provides an efficient approach to solving the problem with rolling hashes, ensuring quick identification of the longest palindromic prefix.
  • It helps you understand how to leverage string matching algorithms, particularly KMP, to avoid redundant checks and optimize the solution.
  • GhostInterview offers insights into the trade-offs between brute-force and optimal approaches for problems involving string manipulation and matching.

Topic Pages

FAQ

What is the main strategy for solving the Shortest Palindrome problem?

The main strategy involves using rolling hashes and string matching algorithms like KMP to identify the longest palindromic prefix and efficiently prepend characters to form the shortest palindrome.

How can I optimize the solution for the Shortest Palindrome problem?

You can optimize the solution by using a rolling hash combined with KMP for efficient string matching, which ensures an O(n) time complexity.

What are the time and space complexities of the solution?

The time complexity is O(n), and the space complexity is O(n), where n is the length of the string.

Can the Shortest Palindrome problem be solved with brute force?

While brute force is possible, it leads to a time complexity of O(n^2), which is inefficient. An optimal solution using rolling hash and KMP is much faster.

What happens if the input string is already a palindrome?

If the string is already a palindrome, no characters need to be added, and the original string will be returned as the shortest palindrome.

GhostInterview Solver

Need direct help with Shortest Palindrome instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Shortest Palindrome from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.