To solve the problem of finding the shortest palindrome by adding characters in front of a given string s, we can utilize the KMP (Knuth-Morris-Pratt) algorithm to efficiently find the longest palindromic prefix of the string. Once we've identified this prefix, the remaining suffix can be reversed and added to the front of s to form the shortest possible palindrome.
Approach:
-
Reverse the String:
- First, reverse the input string
s to obtain rev_s.
-
Concatenate Strings with a Separator:
- Concatenate
s, a unique separator (e.g., #), and rev_s to form a new string combined. The separator ensures that there is no overlap between s and rev_s during the pattern matching process.
-
Compute the KMP Table:
- The KMP table (also known as the "failure function") helps in finding the longest prefix of
combined that is also a suffix. This value indicates the length of the longest palindromic prefix in the original string s.
-
Determine the Characters to Add:
- The characters that need to be added to the front of
s are the reverse of the substring of s that comes after the longest palindromic prefix.
-
Construct the Shortest Palindrome:
- By appending the necessary characters to the front of
s, we obtain the shortest possible palindrome.
C++ Implementation:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string shortestPalindrome(string s) {
if (s.empty()) return "";
// Step 1: Create the concatenated string with a separator
string rev_s = s;
reverse(rev_s.begin(), rev_s.end());
string combined = s + "#" + rev_s;
// Step 2: Compute the KMP table
int n = combined.size();
vector<int> lps(n, 0); // Longest Prefix Suffix
for(int i =1; i < n; ++i){
int len = lps[i-1];
while(len >0 && combined[i] != combined[len]){
len = lps[len-1];
}
if(combined[i] == combined[len]){
len++;
}
lps[i] = len;
}
// Step 3: The longest palindromic prefix length
int palindrome_len = lps.back();
// Step 4: Characters to add in front are the reverse of the remaining suffix
string suffix = s.substr(palindrome_len);
string to_add = suffix;
reverse(to_add.begin(), to_add.end());
// Step 5: Construct the shortest palindrome
return to_add + s;
}
int main() {
// Example 1
string s1 = "aacecaaa";
cout << "Shortest palindrome of \"" << s1 << "\" is \"" << shortestPalindrome(s1) << "\"\n";
// Example 2
string s2 = "abcd";
cout << "Shortest palindrome of \"" << s2 << "\" is \"" << shortestPalindrome(s2) << "\"\n";
// Additional Test Cases
string s3 = "race";
cout << "Shortest palindrome of \"" << s3 << "\" is \"" << shortestPalindrome(s3) << "\"\n";
string s4 = "";
cout << "Shortest palindrome of \"" << s4 << "\" is \"" << shortestPalindrome(s4) << "\"\n";
string s5 = "a";
cout << "Shortest palindrome of \"" << s5 << "\" is \"" << shortestPalindrome(s5) << "\"\n";
return 0;
}
Explanation:
-
Concatenation:
- By concatenating
s and its reverse with a unique separator (#), we ensure that when searching for prefixes and suffixes, there's no false overlap between s and rev_s.
-
KMP Table Construction:
- The
lps array stores the length of the longest proper prefix which is also a suffix for the substring combined[0...i].
- This table helps in identifying the maximum length of the palindromic prefix in
s.
-
Determining the Suffix to Add:
- After computing the
lps array, the last value lps.back() gives the length of the longest palindromic prefix.
- The remaining part of the string (
s.substr(palindrome_len)) is the suffix that isn't part of the palindromic prefix. Reversing this suffix and adding it to the front of s gives the shortest palindrome.
Test Cases:
-
Example 1:
Input: "aacecaaa"
Output: "aaacecaaa"
-
Example 2:
Input: "abcd"
Output: "dcbabcd"
-
Additional Test Cases:
Input: "race"
Output: "ecarace"
Input: ""
Output: ""
Input: "a"
Output: "a"
Complexity Analysis:
-
Time Complexity: O(n), where n is the length of the concatenated string. The KMP algorithm processes each character in constant time.
-
Space Complexity: O(n), due to the storage required for the combined string and the lps array.
This approach ensures that we efficiently find the shortest palindrome by leveraging the properties of the KMP pattern matching algorithm.