<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:
        S = len(s)
        A = [ord(c) - ord('a') + 1 for c in s]
        mod = (1 << 63) - 1

        def robin_karp(threshold):
            visit = defaultdict(list)

            p = pow(26, threshold, mod)
            l = rh = 0
            for i in range(threshold):
                rh = (rh * 26 + A[i]) % mod
            visit[rh].append(0)

            for r in range(threshold, S):
                rh = (rh * 26 + A[r] - A[l] * p) % mod
                l += 1

                if rh in visit:
                    cur = s[l:r+1]
                    for j in visit[rh]:
                        if cur == s[j:j+threshold]:
                            return cur
                visit[rh].append(l)

        lo, hi = 1, S-1
        res = None
        while lo <= hi:
            mid = (lo + hi) // 2
            ans = robin_karp(mid)

            if ans:
                res = ans
                lo = mid + 1
            else:
                hi = mid - 1
        return res if res else ""
24400