<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) {
        int low = 0;
        int high = s.size();
        string ans ="";
        while(low <= high) {
            int mid = (low + high) / 2;
            string t = rolling_hash(s, mid);
            if(t != ""){
                low = mid + 1;
                ans = t;
            }
            else{
                high = mid - 1;
            }
        }

        return ans;
    }

  string rolling_hash(string& str, int& window){
        const long long multiplier=31; // Constant multiplier for hashing
        const long long mod=1000000007; // Modulus for hashing

        // Initialize the hash value for the first window
        long long hashedvalue=0;
        for(int i=0;i<window;i++){
            hashedvalue=(hashedvalue*multiplier+(str[i]-'a'+1))%mod;
        }

        // Precompute the power value to efficiently remove the contribution of the first character
        long long power=1;
        for(int i=1;i<window;i++){
            power=(power*multiplier)%mod;
        }

        // Map to store hash values and their corresponding indices
        unordered_map<long long,vector<int>> mp;
        hashedvalue%=mod;
        mp[hashedvalue].push_back(0);

        // Iterate through the string to calculate hash values for each window
        for(int i=1;i+window<=str.length();i++){
            // Remove the contribution of the first character and update the hash value
            hashedvalue-=((str[i-1]-'a'+1)*power);

            // Ensure hashed value is non-negative
            while(hashedvalue<0)    hashedvalue+=mod;
            hashedvalue%=mod;

            // Multiply by multiplier and add the contribution of the next character
            hashedvalue*=31;
            hashedvalue%=mod;
            hashedvalue+=(str[i+window-1]-'a'+1);
            hashedvalue%=mod;

            // If the hash value already exists in the map, check for potential duplicates
            if(mp.find(hashedvalue)!=mp.end())    {
                for(auto st: mp[hashedvalue]){
                    string old=str.substr(st,window);
                    string newstr=str.substr(i,window);
                    if(old==newstr) return newstr; // Return duplicated substring
                }
            }
            // Update the map with the current hash value and index
            mp[hashedvalue].push_back(i);
        }
        // Return empty string if no duplicated substring found
        return "";
    }
};
423140