<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>

class Solution {
public:
    long long MOD1 = 1e9+33;
    long long MOD2 = 1e9+97;
    string longestDupSubstring(string s) {
        int n = s.length();
        unordered_map<char,int> codes;
        for(int ch = 'a';ch <= 'z';ch++) {
            codes[ch] = ch - 'a' + 1;
        }
        int low = 0,high = n-1;
        string ans;
        while(low <= high){
            int m = low + (high - low)/2;
            bool flag = false;
            long long radix1 = 1,radix2 = 1;
            pair<long long,long long> hash = {0,0};
            for(int i=0;i<m;i++) {
                hash.first = ((hash.first + (codes[s[m-i-1]]*radix1) % MOD1) + MOD1) % MOD1;
                radix1 = (radix1 * 27) % MOD1;
                hash.second = ((hash.second + (codes[s[m-i-1]]*radix2) % MOD2) + MOD2) % MOD2;
                radix2 = (radix2 * 33) % MOD2;
            }
            set<pair<long long,long long>> hashcount;
            hashcount.insert(hash);
            for(int i=0;i<n-m;i++) {
                hash.first = (hash.first * 27) % MOD1;
                hash.first = ((hash.first - (codes[s[i]]*radix1) % MOD1) + MOD1) % MOD1;
                hash.first = (hash.first + codes[s[m+i]]) % MOD1;
                hash.second = (hash.second * 33) % MOD2;
                hash.second = ((hash.second - (codes[s[i]]*radix2) % MOD2) + MOD2) % MOD2;
                hash.second = (hash.second + codes[s[m+i]]) % MOD2;
                if(hashcount.count(hash)) {
                    ans = s.substr(i+1,m);
                    flag = true;
                    break;
                }
                hashcount.insert(hash);
            }
            if(flag) {
                low = m+1;
            }
            else {
                high = m-1;
            }
        }
        return ans;
    }
};
227300