- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1062.java
27 lines (25 loc) · 875 Bytes
/
_1062.java
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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashSet;
importjava.util.Set;
publicclass_1062 {
publicstaticclassSolution1 {
/*
* My completely original solution, although kind of brute-force, on 1/20/2022.
* Two pointer technique:
* j starts from the right, i starts from the left,
* as soon as we are able to find a repeated substring, we return.
*/
publicintlongestRepeatingSubstring(Strings) {
Set<String> seen = newHashSet<>();
for (intj = s.length() - 1; j > 0; j--) {
for (inti = 0; i + j <= s.length(); i++) {
if (!seen.add(s.substring(i, i + j))) {
returnj;
}
}
seen.clear();
}
return0;
}
}
}