forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
longest-repeating-substring.cpp
53 lines (50 loc) · 1.6 KB
/
longest-repeating-substring.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
int longestRepeatingSubstring(string S) {
auto left = 1ul, right = S.length() - 1;
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (!check(S, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
private:
uint64_t check(const string& S, uint64_t L) {
static const uint64_t M = 1e9 + 7;
static const uint64_t D = 26;
uint64_t p = power(D, L, M);
uint64_t curr = 0;
for (uint64_t i = 0; i < L; ++i) {
curr = ((D * curr) + S[i] - 'a') % M;
}
unordered_map<uint64_t, vector<uint64_t>> lookup;
lookup[curr].emplace_back(L - 1);
for (uint64_t i = L; i < S.length(); ++i) {
curr = (D * curr) % M;
curr = (curr + (S[i] - 'a')) % M;
curr = (curr + (M - ((S[i - L] - 'a') * p) % M)) % M;
if (lookup.count(curr)) {
for (const auto& j : lookup[curr]) { // check if string is the same when hash is the same
if (S.substr(j - L + 1, L) == S.substr(i - L + 1, L)) {
return i - L + 1;
}
}
}
lookup[curr].emplace_back(i);
}
return 0;
}
uint64_t power(uint64_t D, uint64_t L, uint64_t M) {
uint64_t result = 1;
for (uint64_t i = 0; i < L; ++i) {
result = (result * D) % M;
}
return result;
}
};