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

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate