File tree 2 files changed +12
-10
lines changed
main/java/com/fishercoder/solutions/secondthousand
test/java/com/fishercoder/secondthousand
2 files changed +12
-10
lines changed Original file line number Diff line number Diff line change 6
6
public class _1062 {
7
7
public static class Solution1 {
8
8
/**
9
- * My completely original, although brute-force solution, on 1/20/2022.
9
+ * My completely original solution, although kind of brute-force, on 1/20/2022.
10
+ * Two pointer technique:
11
+ * j starts from the right, i starts from the left,
12
+ * as soon as we are able to find a repeated substring, we return.
10
13
*/
11
14
public int longestRepeatingSubstring (String s ) {
12
15
Set <String > seen = new HashSet <>();
13
16
for (int j = s .length () - 1 ; j > 0 ; j --) {
14
- int len = j ;
15
- for (int i = 0 ; i <= s .length () - j ; i ++) {
16
- if (!seen .add (s .substring (i , i + len ))) {
17
- return len ;
17
+ for (int i = 0 ; i + j <= s .length (); i ++) {
18
+ if (!seen .add (s .substring (i , i + j ))) {
19
+ return j ;
18
20
}
19
21
}
20
22
seen .clear ();
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .secondthousand ;
2
2
3
3
import com .fishercoder .solutions .secondthousand ._1062 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _1062Test {
10
10
private static _1062 .Solution1 solution1 ;
11
11
12
- @ BeforeClass
13
- public static void setup () {
12
+ @ BeforeEach
13
+ public void setup () {
14
14
solution1 = new _1062 .Solution1 ();
15
15
}
16
16
You can’t perform that action at this time.
0 commit comments