Back to problems

Shortest Palindrome

Algorithm · Reddit · Hard

You have a string s and are allowed to prepend characters to it, but you may not insert characters anywhere else. Produce the shortest palindrome obtainable under that rule. Examples Example 1: Input: s = "aacecaaa" Output: aaacecaaa Example 2: Input: s = "abcd" Output: dcbabcd Constraints 0 <= s.length <= 5 * 10^4 Every character in s is a lowercase English letter.

Checking your access…