Shortest Palindrome

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.

Example 1
Inputs = "aacecaaa"
Output"aaacecaaa"
Adding one a in front of aacecaaa forms the shortest palindrome aaacecaaa.
Example 2
Inputs = "abcd"
Output"dcbabcd"
Adding dcb in front of abcd forms the shortest palindrome dcbabcd.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s consists of lowercase English letters only.

Asked at 9 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate