<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:
        p = 2**63 - 1
        def rabin_karp(mid):
            cur_hash = 0
            for i in range(mid):
                cur_hash = (cur_hash * 26 + nums[i]) %p
            hashes = {cur_hash}
            pos = -1
            max_pow = pow(26,mid,p)
            for i in range(mid,len(s)):
                cur_hash = (26*cur_hash - nums[i-mid]*max_pow + nums[i]) %p 
                if cur_hash in hashes:
                    pos = i - mid + 1
                    break
                hashes.add(cur_hash)
            return pos
        l,r = 0, len(s)-1
        start, end = 0, 0
        nums = [ord(c) - ord('a') for c in s]
        while l <= r:
            mid = l + (r-l)//2
            pos = rabin_karp(mid)
            if pos == -1:
                r = mid - 1
            else:
                start = pos
                l = mid +1
        print(f"mid is{mid} and l is {l}")
        return s[start:start+l-1]

        
21400