<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=(1LL<<31)-1;
    long long base=256;
    string rk(string &s,int mid){
        if(mid==0)return "";   
        unordered_map<long long,int> seen;
        long long h=0;
        long long pow_base=1;
        for(int i=0;i<mid;i++){
            h=(h*base+s[i])%mod;
            pow_base=(pow_base*base)%mod;
        }
        seen[h]=0;
        for(int i=mid;i<s.size();i++){
            h=(h*base+s[i]-s[i-mid]*pow_base%mod+mod)%mod;
            if(seen.find(h)!=seen.end()){
                int ind=seen[h];
                    if(s.compare(ind, mid, s, i - mid + 1, mid) == 0)return s.substr(i-mid+1,mid);
                
            }
            seen[h]=i-mid+1;
        }
        return "";
    }
    string longestDupSubstring(string s) {
        string ans="";
        int n=s.size();
        int l=1,h=n-1;
        while(l<=h){
            int mid=(l+h)/2;
            string dup=rk(s,mid);
            if(dup!=""){
                ans=dup;
                l=mid+1;
            }
            else h=mid-1;
        }
        return ans;}
};
238820