Skip to content

Commit a06e1d9

Browse files
authored
Fixed checks.
1 parent ce18219 commit a06e1d9

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/main/java/g1001_1100/s1042_flower_planting_with_no_adjacent/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import java.util.ArrayList;
77
import java.util.List;
88

9+
@SuppressWarnings("unchecked")
910
public class Solution {
1011
private List<Integer>[] graph;
11-
private int n;
1212
private int[] color;
1313
private boolean[] visited;
1414

src/main/java/g1001_1100/s1044_longest_duplicate_substring/Solution.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,64 @@
88
import java.util.List;
99
import java.util.Set;
1010

11+
@SuppressWarnings("unchecked")
1112
public class Solution {
12-
private final int mod = (int) 1e9 + 7;
13+
private static final int MOD = (int) 1e9 + 7;
1314
private long[] hsh;
1415
private long[] pw;
1516
private final List[] cnt = new List[26];
1617

1718
public String longestDupSubstring(String s) {
18-
int n = s.length(), base = 131;
19-
for (int i = 0; i < 26; i++) cnt[i] = new ArrayList<>();
19+
int n = s.length();
20+
int base = 131;
21+
for (int i = 0; i < 26; i++) {
22+
cnt[i] = new ArrayList<>();
23+
}
2024
hsh = new long[n + 1];
2125
pw = new long[n + 1];
2226
pw[0] = 1;
2327
for (int j = 1; j <= n; j++) {
24-
hsh[j] = (hsh[j - 1] * base + s.charAt(j - 1)) % mod;
25-
pw[j] = pw[j - 1] * base % mod;
28+
hsh[j] = (hsh[j - 1] * base + s.charAt(j - 1)) % MOD;
29+
pw[j] = pw[j - 1] * base % MOD;
2630
cnt[s.charAt(j - 1) - 'a'].add(j - 1);
2731
}
2832
String ans = "";
2933
for (int i = 0; i < 26; i++) {
30-
if (cnt[i].size() == 0) {
34+
if (cnt[i].isEmpty()) {
3135
continue;
3236
}
3337
List<Integer> idx = cnt[i];
3438
Set<Long> set;
35-
int lo = 1, hi = n - idx.get(0);
39+
int lo = 1;
40+
int hi = n - idx.get(0);
3641
while (lo <= hi) {
3742
int len = (lo + hi) / 2;
3843
set = new HashSet<>();
3944
boolean found = false;
4045
for (int nxt : idx) {
41-
if (nxt + len > n) {
42-
break;
43-
}
44-
long substrHash = getSubstrHash(nxt, nxt + len);
45-
if (set.contains(substrHash)) {
46-
found = true;
47-
if (len + 1 > ans.length()) {
48-
ans = s.substring(nxt, nxt + len);
46+
if (nxt + len <= n) {
47+
long substrHash = getSubstrHash(nxt, nxt + len);
48+
if (set.contains(substrHash)) {
49+
found = true;
50+
if (len + 1 > ans.length()) {
51+
ans = s.substring(nxt, nxt + len);
52+
}
53+
break;
4954
}
50-
break;
55+
set.add(substrHash);
5156
}
52-
set.add(substrHash);
5357
}
5458
if (found) {
5559
lo = len + 1;
56-
} else hi = len - 1;
60+
} else {
61+
hi = len - 1;
62+
}
5763
}
5864
}
5965
return ans;
6066
}
6167

6268
private long getSubstrHash(int l, int r) {
63-
return (hsh[r] - hsh[l] * pw[r - l] % mod + mod) % mod;
69+
return (hsh[r] - hsh[l] * pw[r - l] % MOD + MOD) % MOD;
6470
}
6571
}

0 commit comments

Comments
 (0)