<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>

# 0 - 26
# a b a c
# 101110 -> 1011 -> 121011
class Solution:
    def longestDupSubstring(self, s: str) -> str:
        left, right = 0, len(s)
        ret = ""
        mod = pow(2, 63) - 1
        nums = [ord(c) - ord('a') for c in s]
        while left <= right:
            m = (left + right) // 2
            size = m + 1
            h = 0
            hashes = {}
            for i in range(size):
                h = (h * 26 + nums[i]) % mod
            hashes[h] = [0]
            mp = pow(26, size, mod)
            found = ""
            for i in range(1, len(s) - size + 1):
                h = (26 * h - (nums[i - 1] * mp) + nums[i + size - 1]) % mod 
                for j in hashes.get(h, []):
                    if s[j:j + size] == s[i:i + size]:
                        found = s[i:i + size]
                        break
                if not h in hashes:
                    hashes[h] = []
                hashes[h].append(i)
            if len(found) > len(ret):
                ret = found
            if found:
                left = size
            else:
                right = m - 1
        return ret



24800