336. Shortest Palindrome

Given a string s convert it into a palindrome by adding characters to the beginning of it. Return the shortest palindrome you can create by performing this transformation.

Example 1:

Input: s = "aacecaaa"

Output: aaacecaaa

Explanation: Adding "a" to the front of "aacecaaa" makes it a palindrome: "aaacecaaa".

Example 2:

Input: s = "race"

Output: ecarace

Explanation: By adding "eca" in front of "race", the shortest palindrome "ecarace" is formed.

Now Your Turn!

Pick the correct output for the given input

Input: s = "abcd"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution{
public:
string shortestPalindrome(string s) {
}
};
Test Case

Input:

S