-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathShortest Palindrome.cpp
57 lines (45 loc) · 1.71 KB
/
Shortest Palindrome.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
string shortestPalindrome(string s) {
int BASE = 26, MOD = 1e9+7;
int start = s.size()-1;
// Calculate hash values from front and back
long front = 0, back = 0;
long power = 1;
for(int i=0; i<s.size(); i++){
front = (front*BASE + (s[i]-'a'+1))%MOD;
back = (back*BASE + (s[start--]-'a'+1))%MOD;
power = (power*BASE)%MOD;
}
// If hash values of both front and back are same, then it is a palindrome
if(front == back){
return s;
}
// As it is not palindrome, add last characters in the beginning, and then check.
// Store the hash value of the newly added characters from front and back
// new_front will be added to front to get new hash value
// new_back will be added to back to get new hash value
long new_front = 0, new_back = 0;
long new_power = 1;
int end=s.size()-1;
string ans = "";
while(end >= 0){
// Taking character from ending
int ch = (s[end]-'a'+1);
new_front = (new_front*BASE + ch*power) % MOD;
new_back = (ch*new_power + new_back) % MOD;
new_power = (new_power*BASE) % MOD;
int final_front = (new_front + front) % MOD;
back = (back*BASE) % MOD;
int final_back = (new_back + back) % MOD;
// Storing it in separate string
ans += s[end];
end--;
// Both hashes are same
if(final_front == final_back){
break;
}
}
return ans+s;
}
};