-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_1062.java
More file actions
46 lines (41 loc) · 1.31 KB
/
Copy pathP_1062.java
File metadata and controls
46 lines (41 loc) · 1.31 KB
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
package leetcode.medium;
import java.util.HashSet;
import java.util.Set;
@SuppressWarnings("MethodParameterNamingConvention")
public class P_1062 {
private static final int MOD = (int) (1e9 + 7);
private static final int SIZE = 26;
public static int search(int mid, long[] diff, int[] nums) {
final Set<Long> seen = new HashSet<>();
long hash = 0;
for (int i = 0; i < nums.length; i++) {
hash = (hash * SIZE + nums[i]) % MOD;
if (i >= mid) {
hash = Math.floorMod(hash - Math.floorMod(nums[i - mid] * diff[mid], MOD), MOD);
}
if (i >= mid - 1 && !seen.add(hash)) {
return i;
}
}
return -1;
}
public int longestRepeatingSubstring(String S) {
final int n = S.length();
final int[] nums = new int[n];
final long[] diff = new long[n];
for (int i = 0; i < n; i++) {
diff[i] = i == 0 ? 1 : (diff[i - 1] * SIZE) % MOD;
nums[i] = S.charAt(i) - 'a';
}
int lo = 0, hi = n;
while (lo < hi) {
final int mid = (1 + lo + hi) >>> 1;
if (search(mid, diff, nums) != -1) {
lo = mid;
} else {
hi = mid - 1;
}
}
return lo;
}
}