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

using ll = __int128;
using pl = pair<ll,ll> ; 

class Solution {
public:

struct RollingHash{
     
    string s;int n;
    vector<ll> hash, power;
    ll mod = 1e12 + 7;
    ll base = generateRandomPrime();
    

// Function to check if a number is prime
bool isPrime(ll n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (long long i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) return false;
    }
    return true;
}

// Function to generate a random prime number within a given range
ll generateRandomPrime(ll lowerLimit = 31, ll upperLimit  = 1007) {
    std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
    std::uniform_int_distribution<long long> dist(lowerLimit, upperLimit);

    while (true) {
        ll candidate = dist(rng);
        if (isPrime(candidate)) {
            return candidate;
        }
    }
}

    

    
    
    RollingHash(string s){
        this->s = s;
        n = s.size();
        hash.resize(n + 1);
        power.resize(n + 1);
        power[0] = 1;
        for(int i = 1; i <= n; i++){
            power[i] = (power[i - 1] * base) % mod;
        }
        for(int i = 0; i < n; i++){
            hash[i + 1] = (hash[i] * base + s[i]) % mod;
        }
    }

    ll getHash(int l, int r){
        return (hash[r + 1] - hash[l] * power[r - l + 1] % mod + mod) % mod;
    }
  


};



string longestDupSubstring(string s)
{

    ll n = s.size();ll ans = 0;
    ll lo = 0,hi = n-1,mid = 0;RollingHash r(s);

    auto check = [&](ll len){
        
        map<ll,ll> mp; 
        for(ll i=0;i+len<=n;i++)mp[r.getHash(i,i+len-1)]++;

        for(auto&& [x,y] : mp){
            if(y>1){return true;}
        }

        return false;

    };
 
    while(lo<=hi){

         mid  = (lo+hi)/2;
         bool flag  = check(mid);
         if(flag)lo = mid+1,ans = mid;
         else hi = mid-1;

    }
    
    lo = ans;
    if(!lo)return "";
    
    map<ll,pair<ll,ll>> mp;ll id = -1;
    for(ll i=0;i+lo<=n;i++)mp[r.getHash(i,i+lo-1)].first++,mp[(r.getHash(i,i+lo-1))].second=i;
    for(auto &&[x,y] : mp){if(y.first>=2){id = y.second;break;}}
    

    return s.substr(id,lo);

}
};
417380