<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:
        numeric_s = [ord(char) - ord("a") for char in s]

        def find_duplicate_substrings(L: int):
            h = 0
            a = 26
            aL = a ** L

            for i in range(L):
                h = h * a + numeric_s[i]
            
            seen = defaultdict(list)
            seen[h].append(0)

            for start in range(1, len(s) - L + 1):
                h = h * a - numeric_s[start - 1] * aL + numeric_s[start + L - 1]
                if h in seen:
                    current_substring = s[start : start + L]
                    if any(current_substring == s[index : index + L] for index in seen[h]):
                        return start
                
                seen[h].append(start)
            
            return -1
    
        start = -1
        left, right = 1, len(s) - 1
        while left <= right:
            mid = (left + right) // 2
            L = mid

            start_of_duplicate = find_duplicate_substrings(L)

            if start_of_duplicate != -1:
                left = mid + 1
                start = start_of_duplicate
            else:
                right = mid - 1
        
        return s[start : start + left - 1]
158400