-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19-LongestDuplicateSubstring.cpp
More file actions
33 lines (30 loc) · 837 Bytes
/
19-LongestDuplicateSubstring.cpp
File metadata and controls
33 lines (30 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
string longestDupSubstring(string S) {
string_view longest;
unordered_set<string_view> set;
size_t beg = 1;
size_t end = S.size() - 1;
while (beg <= end)
{
auto len = beg + (end - beg) / 2;
bool found = false;
for (size_t i = 0; i != S.size() - len + 1; ++i)
{
const auto [it, inserted] = set.emplace(S.data() + i, len);
if (!inserted)
{
found = true;
longest = *it;
break;
}
}
if (found)
beg = len + 1;
else
end = len - 1;
set.clear();
}
return {longest.begin(), longest.end()};
}
};