<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:
        def check(size):
            MOD = 10 ** 9 + 7
            curr = 0
            highest_bit = 1
            for i in range(size - 1):
                highest_bit = highest_bit * 26 % MOD
            pos_map = defaultdict(list)
            for i in range(len(s)):
                curr = (curr * 26 + ord(s[i])) % MOD
                if i >= size - 1:
                    j = i - (size - 1)
                    if curr in pos_map:
                        for k in pos_map[curr]:
                            if s[j:j + size] == s[k:k + size]:
                                return s[j:j + size]
                    pos_map[curr].append(j)
                    curr = (curr + MOD - highest_bit * ord(s[j]) % MOD) % MOD
            return ''

        lo, hi = 1, len(s)
        r = ''
        while lo < hi:
            mid = (lo + hi) // 2
            candidate = check(mid)
            if candidate:
                r = candidate
                lo = mid + 1
            else:
                hi = mid
        return r



23600