-
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
package com.fishercoder.solutions.secondthousand;
import java.util.HashSet;
import java.util.Set;
public class _1062 {
public static class Solution1 {
/*
* 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.
*/
public int longestRepeatingSubstring(String s) {
Set<String> seen = new HashSet<>();
for (int j = s.length() - 1; j > 0; j--) {
for (int i = 0; i + j <= s.length(); i++) {
if (!seen.add(s.substring(i, i + j))) {
return j;
}
}
seen.clear();
}
return 0;
}
}
}