<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:
    def longestDupSubstring(self, s: str) -> str:
        print(f"case={s}")
        MOD = 2**63-1
        n = len(s)
        pre_hash = [0]*(n+1)
        BASE = randint(8*10**9,9*10**9)
        for i,c in enumerate(s):
            pre_hash[i+1] = (pre_hash[i]*BASE + ord(c))%MOD
        res = ""
        def sub_hash(l,r):
            return (pre_hash[r] - pre_hash[l] * pow(BASE,r-l,MOD))%MOD
        self.start = 0; self.maxlen = 0
        def test(curlen):
            seen = {}
            for i in range(n-curlen+1):
                cur_hash = sub_hash(i,i+curlen)
                #print(f"l={i},r={i+curlen},substr={s[i:i+curlen]},hash={cur_hash}")
                if cur_hash in seen:
                    self.start = seen[cur_hash]
                    self.maxlen = curlen
                    #print(f"start={self.start},maxlen={self.maxlen}")
                    return True
                seen[cur_hash] = i
            return False
        left = 1; right = n
        while left < right:
            mid = left + (right-left+1)//2
            #print(f"length tested={mid}")
            if test(mid):
                left = mid
            else:
                right = mid-1
        test(left)
        #print(f"Just before return:start={self.start},maxlen={self.maxlen}")
        return s[self.start:self.start+self.maxlen]
            
22800