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

#define ull unsigned long long
class Solution {
public:
    
    bool solve(int len, ull power,string s, int &a, int &b ){
        ull hash=0;
        for(int i=0;i<len;i++) hash=hash*131+s[i];
        unordered_set<ull> st;
        st.insert(hash);
        for(int i=len;i<s.size();i++){
            hash=(hash-power*s[i-len])*131+s[i];
            if(st.find(hash)!=st.end()) {
                if(b<len){
                    a=i-len+1;
                    b=len;
                }
                return true;
            };
            st.insert(hash);

        }
        return false;
    }


    string longestDupSubstring(string s) {
        int n=s.size();
        vector<ull> power(n,1);
        for(int i=1;i<n;i++) power[i]=power[i-1]*131;
        int l=0,r=n-1;
        int a=0,b=0;
        while(l<=r){
            int mid=l+(r-l)/2;
            if(solve(mid+1,power[mid],s,a,b)) l=mid+1;
            else r=mid-1;
        }
        return s.substr(a,b);

    }
};
215780