<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:
        n = len(s)
        base = 26
        mod = 10 ** 9 + 7
        nums = [ord(c) - ord('a') for c in s]
        bs = -1
        left, right = 1, n - 1
        while left <= right:
            mid = (left + right) // 2
            start = self.find(mid, base, mod, n, nums)
            if start != -1:
                left = mid + 1
                bs = start
            else:
                right = mid - 1
                
        return s[bs: bs + left - 1] if bs != -1 else ""
    
    def find(self, sub_len: int, base: int, mod: int, text_len: int, nums: List[int]) -> int:
        h = 0
        for i in range(sub_len):
            h = (h * base + nums[i]) % mod
        seen = defaultdict(list)
        seen[h].append(0)
        
        base_pow = pow(base, sub_len, mod)
        for start in range(1, text_len - sub_len + 1):
            h = (h * base - nums[start - 1] * base_pow + nums[start + sub_len - 1]) % mod
            if h in seen:
                curr = nums[start: start + sub_len]
                if any(curr == nums[ind: ind + sub_len] for ind in seen[h]):
                    return start
            seen[h].append(start)
        return -1
24200