The Indian Engineer

Problem 214 Shortest Palindrome

Posted on 5 mins

String Rolling Hash String Matching Hash Function

Problem Statement

Link - Problem 214

Question

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:

Solution

class Solution {
public:
    string shortestPalindrome(string s) {
        int size = s.size();
        if(size == 0){
            //cout<<s<<endl;
            return s;
        }

        int l =0, r;
        for(r =size-1; r>=0; r--){
            if(s[r]==s[l])
                l++;
        }

        if(l == size){
            //cout<<s<<endl;
            return s;
        }

        string rightnonmatch = s.substr(l,size);
        string reverseprepend = string(rightnonmatch.rbegin(),rightnonmatch.rend());

        //cout<<"nonmatchsuffix "<<rightnonmatch<<" reversedsuffix "<<reverseprepend<<endl;

        string ans = reverseprepend + shortestPalindrome(s.substr(0,l)) + rightnonmatch;
        //cout<< ans<<endl;
        return ans;
    }
};

Complexity Analysis

| Algorithm     | Time Complexity | Space Complexity |
| ------------- | --------------- | ---------------- |
| Recursive KMP | O(n log n)      | O(n)             |

Explanation

Intial Thoughts

To approach this problem, consider the concept of a palindrome and how it can be achieved by adding characters to the front of a given string. Think about how to identify the non-matching part of the string that needs to be mirrored. Consider the idea of comparing characters from both ends of the string and moving towards the center. Think about how to handle the case when the input string is already a palindrome. Identify key factors such as the length of the string and the concept of reversal.

Intuitive Analysis

Intuitively, to find the shortest palindrome, start by comparing characters from the beginning and end of the string, moving inwards. When a mismatch is found, reverse the non-matching part and prepend it to the original string, but also consider the part of the string before the mismatch. Think of the process as folding the string in half and then adjusting the non-matching parts to create a palindrome. Consider how this process can be applied recursively to handle different cases. Consider how to handle the remaining part of the string after reversing the non-matching part. Think about how to combine these insights to form an efficient solution.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Hard difficulty.


Similar Questions:

Title URL Difficulty
Longest Palindromic Substring https://leetcode.com/problems/longest-palindromic-substring Medium
Find the Index of the First Occurrence in a String https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string Easy
Palindrome Pairs https://leetcode.com/problems/palindrome-pairs Hard
Maximum Deletions on a String https://leetcode.com/problems/maximum-deletions-on-a-string Hard