<p>Given a string <code>s</code>, consider all <em>duplicated substrings</em>: (contiguous) substrings of s that occur 2 or more times.&nbsp;The occurrences&nbsp;may overlap.</p>

<p>Return <strong>any</strong> duplicated&nbsp;substring that has the longest possible length.&nbsp;If <code>s</code> does not have a duplicated substring, the answer is <code>&quot;&quot;</code>.</p>

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

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

typedef long long ll;

class Solution {
public:

    int hBase = 31;
    int hBase2 = 37;
    int hMod = 1000000009;

    string longestDupSubstring(string s) {
        int n = s.size();
        vector< ll > hBaseV(n); hBaseV[0] = 1;
        vector< ll > hBaseV2(n); hBaseV2[0] = 1;
        for( int i =1; i<n; ++i ){
            hBaseV[i] = hBaseV[i-1];
            hBaseV[i] *= hBase; 
            hBaseV[i] %= hMod;
            //
            hBaseV2[i] = hBaseV2[i-1];
            hBaseV2[i] *= hBase2; 
            hBaseV2[i] %= hMod; 
        }

        int l = 0, r = n-1;
        string ans = "";
        while ( l < r ) {
            int len = ( l+r+1 )/2;
            unordered_map< ll, bool > um, um2; 
            ll runHash = 0;
            ll runHash2 = 0;
            for ( int i = 0; i<len; i++ ){
                ll base = (s[ i ] - 'a') + 1;
                runHash *= hBase; runHash %=hMod; 
                runHash += base; runHash %= hMod;
                //
                runHash2 *= hBase2; runHash2 %=hMod; 
                runHash2 += base; runHash2 %= hMod;
            }
            um[runHash] = true;
            um2[runHash2] = true;
            int runI = len;

            bool ansf = false;
            while( runI < n ) {
                ll sub = ((s[ runI - len ] - 'a') + 1); sub *= hBaseV[ len-1 ]; sub %= hMod;
                ll sub2 = ((s[ runI - len ] - 'a') + 1); sub2 *= hBaseV2[ len-1 ]; sub2 %= hMod;

                ll add = ((s[ runI ] - 'a') + 1);
                runHash -= sub; runHash += hMod; runHash %= hMod;
                runHash *= hBase; runHash %= hMod;
                runHash += add; runHash %= hMod;
                //
                runHash2 -= sub2; runHash2 += hMod; runHash2 %= hMod;
                runHash2 *= hBase2; runHash2 %= hMod;
                runHash2 += add; runHash2 %= hMod;

                if ( um[ runHash ] == true && um2[ runHash2 ] == true){
                    ans = s.substr( runI-len +1, len );
                    ansf = true;
                    break;
                }
                um[runHash] = true;
                um2[runHash2] = true;
                runI++;
            }
            if ( ansf ) l = len;
            else r = len-1;
        }
        return ans;
    }
};
457700