<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:
        def search(length: int) -> str:
            """Search for a duplicated substring of given length."""
            base = 26  # Base for the rolling hash function (number of possible characters)
            mod = 2**63 - 1  # Large prime modulus to avoid collisions
            current_hash = 0
            base_l = pow(base, length, mod)  # Base^length % mod
            seen = set()

            for i in range(length):
                current_hash = (current_hash * base + ord(s[i])) % mod

            seen.add(current_hash)

            for start in range(1, len(s) - length + 1):
                current_hash = (current_hash * base - ord(s[start - 1]) * base_l + ord(s[start + length - 1])) % mod
                if current_hash in seen:
                    return s[start:start + length]
                seen.add(current_hash)
            
            return ""

    # Binary search on length of the substring
        left, right = 1, len(s)
        result = ""

        while left <= right:
            mid = (left + right) // 2
            substring = search(mid)
            if substring:
                result = substring
                left = mid + 1
            else:
                right = mid - 1

        return result
20700