-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path214_Shortest Palindrome.cpp
More file actions
28 lines (23 loc) · 929 Bytes
/
214_Shortest Palindrome.cpp
File metadata and controls
28 lines (23 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
// For example:
// Given "aacecaaa", return "aaacecaaa".
// Given "abcd", return "dcbabcd".
class Solution { // KMP, O(n)
public:
string shortestPalindrome(string s) {
string rev_s(s);
reverse(rev_s.begin(), rev_s.end());
string temp = s + '/' + rev_s;
vector<int> kmp(temp.size());
int j = 0; // index of prefix
for (int i = 1; i < temp.size(); ++i) {
if (temp[i] == temp[j]) {
kmp[i] = ++j;
} else {
while (j > 0 && temp[i] != temp[j]) j = kmp[j - 1];
kmp[i] = (temp[i] == temp[j]) ? ++j : 0;
}
}
return rev_s.substr(0, rev_s.size() - kmp.back()) + s;
}
};