<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:
        n = len(s)
        ans = ""
        # binary search
        l = 1
        r = n
        hash_map = {}
        while l <= r:
            mid = (l+r)//2
            hash_map_new = {}
            found = ""
            for i in range(n-mid+1):
                if s[i:i+mid] in hash_map_new:
                    found = s[i:i+mid]
                    break
                hash_map_new[s[i:i+mid]] = True
            if len(found) > 0:
                ans = found
                l = mid+1
            else:
                r = mid-1
        return ans






        # n = len(s)
        # hash_map = {}
        # for i in range(n):
        #     if s[i] not in hash_map:
        #         hash_map[s[i]] = []
        #     hash_map[s[i]].append(i)
        # ans = ""
        # while len(hash_map) > 0:
        #     hash_map = {k:v for k, v in hash_map.items() if len(v)>1}
        #     hash_map_new = {}
        #     for k, v in hash_map.items():
        #         ans = k
        #         for i in range(len(v)):
        #             if v[i]+1 >= n:
        #                 continue
        #             s_ = k + s[v[i]+1]
        #             if s_ not in hash_map_new:
        #                 hash_map_new[s_] = []
        #             hash_map_new[s_].append(v[i]+1)
        #     hash_map = hash_map_new
        # return ans
                            
239300