<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 {
    
    long long base = 31, MOD=1e11+19;
    
public:
    string longestDupSubstring(string s) {
        
        int n = s.size();
        int l=0, r=n;
        int mx=0,idx=0;
        
        while(l<r){
            
            int mid = l+ceil((r-l)/(2*1.0));
            
            unordered_set<long long> currHash;
            long long hash=0, d=1;
            bool found = false;
            for(int i=0; i<n; i++){
                hash = (hash*base + s[i]-'a'+1)%MOD;
                if(i>=mid){
                    hash = (MOD + hash - d*(s[i-mid]-'a'+1)%MOD)%MOD;
                }else{
                    d = d*base%MOD;
                }
                
                if(i>=mid-1){
                    if(currHash.count(hash)){
                        idx = i-mid+1;
                        found = true;
                        break;
                    }
                    currHash.insert(hash);
                }
            }
            
            if(!found)
                r = mid-1;
            else l = mid;
        }
        string ans = "";
        
        
        for(int i = idx; i<(idx+l); i++){
            ans += s[i];
        }
        
        return ans;
    }
};
192740