<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:
        MODS = 10 ** 9 + 7
        n = len(s)
        def check(s1, s2):
            for a,b in zip(s1, s2):
                if a != b:
                    return False
            return True
            
        def search_helper(l):
            h = (26 ** (l-1)) % MODS
            res = collections.defaultdict(list)
            cur = 0
            start = 0
            for i in range(start, start + l):
                cur = (26 * cur + (ord(s[i]) - 97)) % MODS
            res[cur].append(s[start:(start+l)])
            while start < n - l:
                cur = ((cur - (ord(s[start]) - 97) * h) * 26 + ord(s[start + l]) - 97) % MODS
                start += 1
                new = s[start:(start+l)]
                if cur in res:
                    for t in res[cur]:
                        if check(t, new):
                            return new
                res[cur].append(new)
            return ""
        left, right, ans = 1, n-1, ""
        while left <= right:
            mid = (left + right) // 2
            pattern = search_helper(mid)
            if pattern:
                ans = pattern
                left = mid + 1
            else:
                right = mid - 1
        return ans




        
240700