<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):
            """
            Binary search helper function to check if there's any duplicate substring of length L.
            Using a rolling hash to find it efficiently.
            """
            # Rolling hash parameters
            base = 26
            mod = 2**63 - 1
            cur_hash = 0
            baseL = pow(base, L, mod)  # Base^L % mod
            seen_hashes = {}
            
            # Compute the initial hash for the first substring of length L
            for i in range(L):
                cur_hash = (cur_hash * base + ord(s[i]) - ord('a')) % mod
            
            seen_hashes[cur_hash] = 0  # Storing the starting index
            
            for start in range(1, len(s) - L + 1):
                # Roll the hash to remove the previous starting character and add the new one
                cur_hash = (cur_hash * base - (ord(s[start - 1]) - ord('a')) * baseL + (ord(s[start + L - 1]) - ord('a'))) % mod

                if cur_hash in seen_hashes:
                    # Verify and verify that it's not a collision
                    prev_start = seen_hashes[cur_hash]
                    if s[start:start + L] == s[prev_start:prev_start + L]:
                        return start
                seen_hashes[cur_hash] = start
            
            return -1
        
        left, right = 1, len(s)
        start_index = -1
        
        while left < right:
            mid = (left + right) // 2
            start = search(mid)
            
            if start != -1:
                start_index = start
                left = mid + 1
            else:
                right = mid
        
        if start_index != -1:
            return s[start_index:start_index + left - 1]
        return ""

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