<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 {
public:
    string longestDupSubstring(string S) {
        int start = 0;
        int end = S.size()-1;
        string_view ans;
        unordered_set<string_view> seen;
        while (start <= end){
            int len = start + ((end-start)>>1);
            bool found = false;
            for (int i = 0; i < S.size()+1-len; i++){             
                string_view str = string_view(S).substr(i, len);   
                if (seen.count(str)){                              
                    ans = str;
                    found = true;
                    break;
                } else
                    seen.emplace(str);
            }
            if (found)
                start = len+1;
            else
                end = len-1;       
            seen.clear();           
        }
        return {ans.begin(),ans.end()};    
    }
};
123620