15% off all plans & roadmaps Use code
See plansLeetCode 214 · Solution & editorial
You are given a string s. You can convert s to a palindrome by adding characters in front of it.
Shortest Palindrome is a String Manipulation Patterns problem, in the Naive / KMP / Rabin-Karp subpattern. The pattern covers palindrome checking, anagram detection, pattern matching, and string conversion
Master string matching algorithms including naive search, KMP pattern matching, and Rabin-Karp hashing.
The reference solution runs in O(n) time and O(n) space.
Shortest Palindrome has been reported in interviews at Microsoft.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Shortest Palindrome is a String Manipulation Patterns problem, in the Naive / KMP / Rabin-Karp subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.