LeetCode 214, Hard. Topics: String, Rolling Hash, String Matching, Hash Function, Manacher, Z Algorithm, Knuth-Morris-Pratt Algorithm. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:
Problem
You are given a string s. You can convert s to a palindrome by adding characters in front of it.
Return the shortest palindrome you can find by performing this transformation.
Examples
Constraints
- 0 <= s.length <= 5 * 10^4
- s consists of lowercase English letters only.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026