<p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>

<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aacecaaa"
<strong>Output:</strong> "aaacecaaa"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "abcd"
<strong>Output:</strong> "dcbabcd"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>s</code> consists of lowercase English letters only.</li>
</ul>


#define ll long long

class Solution {
public:
    ll p = 31;
    ll m = 1e9+9;

    vector<ll> p_pow;
    vector<ll> h1, h2;

    void compute_power(int n) {
        p_pow.resize(n + 1);
        p_pow[0] = 1;
        for (int i = 1; i <= n; i++) {
            p_pow[i] = (p_pow[i - 1] * p) % m;
        }
    }

    void compute_prefix_hashes(const string &s, vector<ll> &h) {
        int n = s.size();
        h.resize(n + 1);
        h[0] = 0;
        for (int i = 0; i < n; i++) {
            h[i + 1] = (h[i] * p + (s[i] - 'a' + 1)) % m;
        }
    }

    ll get_hash(const vector<ll> &h, int l, int r) {
        ll hash_value = (h[r + 1] - (h[l] * p_pow[r - l + 1] % m) + m) % m;
        return hash_value;
    }

    string shortestPalindrome(string s) {
        int n = s.size();
        if (n == 0) return "";

        compute_power(n);

        string s_rev = s;
        reverse(s_rev.begin(), s_rev.end());

        compute_prefix_hashes(s, h1);
        compute_prefix_hashes(s_rev, h2);
        int maxLen = 0;
        for (int i = 0; i < n; i++) {
            if (get_hash(h1, 0, i) == get_hash(h2, n - i - 1, n - 1)) {
                maxLen = i + 1;
            }
        }

        string to_add = s.substr(maxLen);
        reverse(to_add.begin(), to_add.end());

        return to_add + s;
    }
};


13700