Talentd
FleetCode

214. Shortest Palindrome

Hard43.3% acceptance8 companies

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:

Input: s = "aacecaaa"
Output: "aaacecaaa"

Example 2:

Input: s = "abcd"
Output: "dcbabcd"

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of lowercase English letters only.
22:07
Still stuck?Read 4 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def shortestPalindrome(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"aacecaaa"
Input
"abcd"
Your code is saved on this device.Solution