<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:
        arr = [ord(c) - ord('a') for c in s]
        MOD = 2 ** 63 - 1
        def test(L):
            mul = pow(26, L, MOD)
            cur = reduce(lambda x, y: (26 * x + y) % MOD, arr[:L])
            seen = {cur}
            for i in range(L, len(arr)):
                cur = (26 * cur + arr[i] - arr[i-L] * mul) % MOD
                if cur in seen:
                    return i - L + 1
                seen.add(cur)
            return None
        
        res, lo, hi = 0, 0, len(arr)
        while lo < hi:
            mid = (lo + hi + 1) // 2
            pos = test(mid)
            if pos:
                lo = mid
                res = pos
            else:
                hi = mid - 1
        return s[res:res+lo]
21500