<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(L: int) -> int:
            """Search for a duplicated substring of length L.
            Return the start index if found, otherwise return -1."""
            MOD = 2**63 - 1
            base = 26
            current_hash = 0
            
            # Compute the hash of the first L-length substring
            for i in range(L):
                current_hash = (current_hash * base + nums[i]) % MOD
            hash_set = {current_hash}
            
            # Precompute base^L % MOD for rolling hash
            base_L = pow(base, L, MOD)
            
            for start in range(1, n - L + 1):
                # Rolling hash compute: remove the first char, add the new char
                current_hash = (current_hash * base - nums[start - 1] * base_L + nums[start + L - 1]) % MOD
                if current_hash in hash_set:
                    return start
                hash_set.add(current_hash)
                
            return -1

        n = len(s)
        nums = [ord(c) - ord('a') for c in s]

        # Binary search on the length of the duplicate substring
        left, right = 1, n
        start = 0
        while left < right:
            mid = left + (right - left) // 2
            idx = search(mid)
            if idx != -1:  # Found a duplicated substring of length mid
                start = idx
                left = mid + 1
            else:
                right = mid
        
        return s[start:start + left - 1]

# Example usage:
# sol = Solution()
# print(sol.longestDupSubstring("banana"))  # Output: "ana"
# print(sol.longestDupSubstring("abcd"))    # Output: ""
21300