<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 mod = 1e9+9;
    bool compare(string& s, int i, int j, int len) {
        for(int k = 0; k < len; k++, i++, j++) {
            if(s[i] != s[j]) return false;
        }
        return true;
    }
    string longestDupSubstring(string s) {
        long long l = 1, h = s.length(), n = s.length(), prime = 31;
        vector<long long> p(n, 1);
        for(long long i = 1; i < n; i++) {
            p[i] = (1LL*(p[i-1]%mod)*prime)%mod;
        }
        string ans = "";
        while(l <= h) {
            // cout << l << " " << h << endl;
            long long len = l + (h - l)/2;
            // cout << len << endl;
            bool flag = false;
            /*
                // dont use this. use rolling hash
                
                unordered_set<string> freq;
                for(int i = 0; i < n-len+1; i++) {
                    string x = s.substr(i, len);
                    if(freq.find(x) != freq.end()) {
                        flag = true;
                        ans = x;
                        break;
                    }
                    freq.insert(x);
                }
            */

            unordered_map<long long, long long> freq;
            long long hash = 0;
            for(long long i = 0; i < len; i++) {
                hash = (hash%mod + (p[len-i-1]*(s[i]-'a'+1))%mod)%mod;
            }
            freq[hash] = 0;
            for(long long i = 0, j = len; j < n; i++, j++) {
                // cout << s[i] << " " << s[j] << endl;
                hash = (1LL*hash + 1LL*mod - (1LL*p[j-i-1]*(s[i]-'a'+1))%mod )%mod;
                hash = ((hash%mod)*prime)%mod;
                hash += (long long)(s[j]-'a'+1);
                if(freq.find(hash) != freq.end()) {
                    if(compare(s, freq[hash], i+1, len)) {
                        // cout << s.substr(i+1, len) << endl;
                        flag = true;
                        ans = s.substr(i+1, len);
                        break;
                    }
                }
                freq[hash] = i+1;
            }
            if(flag) {
                l = len + 1;
            } else {
                h = len - 1;
            }
        }
        return ans;
    }
};
261860