<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 = tuple(ord(c) - ord("a") + 1 for c in s)
        base = len(set(s))
        prime = 2**31 - 1

        lo, hi = 0, len(s)
        duplicates = {}
        longest = 0
        while lo < hi:
            window_size = int((hi - lo) / 2) + lo

            h = 0
            for i in range(window_size):
                h = (h * base + s[i]) % prime

            hashes = defaultdict(set)
            hashes[h].add(0)
            dup = None
            first_multiplier = base ** (window_size - 1) % prime
            for i in range(1, len(s) - window_size + 1):
                h -= s[i - 1] * first_multiplier
                h *= base
                h += s[i + window_size - 1]
                h %= prime

                if h in hashes:
                    substring = s[i : i + window_size]
                    if any(substring == s[j : j + window_size] for j in hashes[h]):
                        dup = substring
                        break
                else:
                    hashes[h].add(i)

            if dup:
                duplicates[window_size] = dup
                longest = max(longest, window_size)
                lo = window_size + 1
            else:
                hi = window_size

        if duplicates:
            return "".join(chr(c + ord("a") - 1) for c in duplicates[longest])

        return ""

28100