<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) {
        vector<int> next;
        int L = 0;
        int first;
        int longest_pos = 0;
        
        next.resize(s.size());
        for (int i = 0; i < s.size(); ++i)
            next[i] = i + 1;
        next[s.size() - 1] = -1;
        
        bool exists_shift = true;
        while (exists_shift) {
            exists_shift = false;
            
            ++L;
            for (int i = 0; i < s.size(); ++i) {
                while (next[i] != -1) {
                    int j = next[i];
                    if (j >= s.size()) {
                        next[i] = -1;
                        break;
                    }
                    
                    if (s[i + L - 1] == s[j + L - 1]) {
                        longest_pos = i;
                        exists_shift = true;
                        break;
                    }
                    
                    next[i] = next[j];
                }
            }
        }
        
        return s.substr(longest_pos, L - 1);
    }
};
14180