<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 __init__(self):
        self.ans = ""

    def check(self, s, k):
        N = len(s)
        records = set()
        word = s[0:k]
        records.add(word)
        for i in range(k, N):
            word = word[1:]
            word += s[i]
            if word in records:
                self.ans = word
                return True
            records.add(word)
        return False

    def longestDupSubstring(self, s: str) -> str:
        left, right = 0, len(s)
        while left < right:
            mid = (left + right) // 2
            if self.check(s, mid):
                left = mid + 1
            else:
                right = mid
        return self.ans
239900