<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.p = 41

    def duplicate(self, s, mid):
        h = {}
        init_hash = 0
        factor = 1
        for i in range(mid-1, -1, -1):
            init_hash += factor*ord(s[i])
            factor *= self.p
        
        factor = factor//self.p
        h[init_hash] = 1
        for i in range(1, len(s)-mid+1):
            init_hash = (init_hash - ord(s[i-1])*factor)*self.p + ord(s[i+mid-1])
            if init_hash in h:
                return s[i: i+mid]
            else:
                h[init_hash] = 1
        return None

    def longestDupSubstring(self, s: str) -> str:
        low, high = 0, len(s)

        result = ""
        while(low < high):
            mid = (low + high)//2
            candidate = self.duplicate(s, mid)
            if candidate and len(candidate) != 0:
                result = candidate
                low = mid+1
            else:
                high = mid

        return result
176200