<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)
        left = 0
        right = n - 1
        len_to_str = collections.defaultdict(int)
        
        def find_duplicate(length):
            base = 31
            hash_base = base ** length
            
            hash = 0
            target = set()
            for i in range(n):
                hash = hash * base + (ord(s[i]) - ord('a'))
                if i >=length:
                    hash = hash - hash_base * (ord(s[i-length]) - ord('a'))
                if i >= length -1:
                    if hash in target:
                        
                        len_to_str[length] = i-length+1
                        return True
                    target.add(hash)
            return False
                    
            
        while left < right:
            mid = left + (right - left + 1)//2
            if find_duplicate(mid):
                left = mid
            else:
                right = mid - 1
        
        if find_duplicate(left):
            return s[len_to_str[left]:left+len_to_str[left]]
        else:
            return ""
165400