3
string shortestPalindrome(string s) {4
int BASE = 26, MOD = 1e9 + 7;5
int start = s.size() - 1;7
// Calculate hash values from front and back8
long front = 0, back = 0;11
for (int i = 0; i < s.size(); i++) {12
front = (front * BASE + (s[i] - 'a' + 1)) % MOD;13
back = (back * BASE + (s[start--] - 'a' + 1)) % MOD;14
power = (power * BASE) % MOD;17
// If hash values of both front and back are same, then it is a palindrome22
// As it is not palindrome, add last characters in the beginning, and then23
// check. Store the hash value of the newly added characters from front and26
// new_front will be added to front to get new hash value27
// new_back will be added to back to get new hash value28
long new_front = 0, new_back = 0;31
int end = s.size() - 1;35
// Taking character from ending36
int ch = (s[end] - 'a' + 1);38
new_front = (new_front * BASE + ch * power) % MOD;39
new_back = (ch * new_power + new_back) % MOD;40
new_power = (new_power * BASE) % MOD;42
int final_front = (new_front + front) % MOD;43
back = (back * BASE) % MOD;44
int final_back = (new_back + back) % MOD;46
// Storing it in separate string50
// Both hashes are same51
if (final_front == final_back) {