<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:
    string longestDupSubstring(string s) {
        const int modulo = 1000000007;
        int l = 0 , r = s.length()-1;
        int max_len = 0, start = 0;
        unordered_map<int,int> mp;
        long long pow_31[s.length()];
        pow_31[0] = 1;
        for (int i = 1 ; i < s.length() ; i++)
        {
            pow_31[i] = (31 * pow_31[i-1])%(1000000007);
        }
        while (l <= r)
        {
            mp.clear();
            int m = (l+r)>>1;
            
            long long hash = 0;
            for (int i = 0 ; i < m-1 ; i++)
            {
                hash = (31*hash + s[i])%modulo;
            } 
            // cout << m << " ";
            for (int curr = m - 1; curr < s.length() ; curr++)
            {
                hash = (31*hash + s[curr])%modulo;
                // cout << hash << " ";
                if (mp.find(hash) != mp.end() && s.substr(curr-m+1,m) == s.substr(mp[hash],m))
                {
                    max_len = m;
                    start = curr - m + 1;
                    break;
                }
                else
                {
                    mp.insert({hash,curr-m+1});
                }
                hash = ((hash - ((s[curr-m+1])*pow_31[m-1])%modulo)%modulo + modulo)%modulo;
            }
            // cout << "\n";
            if (max_len == m)
            {
                l = m+1;
            }
            else
            {
                r = m - 1;
            }
            
        }
        return s.substr(start,max_len);
    }
};
71780