<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>

from collections import defaultdict
class Solution:
    def longestDupSubstring(self, s: str) -> str:
        def RabinKarp(text, M, q):
            if M == 0:
                return True
            h, t, d = pow(256, M - 1, q), 0, 256
            
            dic = defaultdict(list)

            for i in range(M):
                t = (d * t + ord(text[i])) % q
            dic[t].append(0)
            
            for i in range(len(text) - M):
                t = (d * (t - ord(text[i]) * h) + ord(text[i + M])) % q
                for j in dic[t]:
                    if text[i + 1: i + M + 1] == text[j:j + M]:
                        return (True, text[j:j + M])
                dic[t].append(i + 1)
            return (False, "")
        
        beg, end = 0, len(s)
        q = 2**31 - 1
        Found = ""

        while beg + 1 < end:
            mid = (beg + end) // 2
            isFound, candidate = RabinKarp(s, mid, q)
            if isFound:
                beg, Found = mid, candidate
            else:
                end = mid
        return Found
23200