Skip to main content

Shortest Palindrome

Problem​

Given a string s, you can convert it 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×1040 \leq \text{s.length} \leq 5 \times 10^4
  • s consists of lowercase English letters only.

Solution​

To solve this problem efficiently, we can leverage the KMP (Knuth-Morris-Pratt) algorithm to find the longest prefix of the reversed string that matches the suffix of the original string. This allows us to determine the shortest palindrome.

Steps:​

  1. Reverse the string s to get rev_s.
  2. Construct a new string new_s = s + "#" + rev_s.
  3. Compute the KMP table (prefix function) for new_s to find the longest prefix which is also a suffix.
  4. The length of this longest prefix gives us the length of characters from rev_s that we need to prepend to s to form the shortest palindrome.

Code in Different Languages​

Written by @mahek0620

class Solution(object):
def shortestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return ""

# Step 1: Reverse the string
rev_s = s[::-1]

# Step 2: Combine s and rev_s with a separator
new_s = s + "#" + rev_s

# Step 3: Compute KMP table (prefix function) for new_s
n = len(new_s)
kmp = [0] * n

for i in range(1, n):
j = kmp[i - 1]
while j > 0 and new_s[i] != new_s[j]:
j = kmp[j - 1]
if new_s[i] == new_s[j]:
j += 1
kmp[i] = j

# Step 4: Calculate the overlap length
overlap = kmp[-1]

# Step 5: Form the result palindrome
return rev_s[:len(s) - overlap] + s

References​