Skip to content

Commit 91888f7

Browse files
update 1062
1 parent 36553b0 commit 91888f7

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/main/java/com/fishercoder/solutions/secondthousand/_1062.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
public class _1062 {
77
public static class Solution1 {
88
/**
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.
1013
*/
1114
public int longestRepeatingSubstring(String s) {
1215
Set<String> seen = new HashSet<>();
1316
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;
1820
}
1921
}
2022
seen.clear();

src/test/java/com/fishercoder/secondthousand/_1062Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.fishercoder.secondthousand;
22

33
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;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _1062Test {
1010
private static _1062.Solution1 solution1;
1111

12-
@BeforeClass
13-
public static void setup() {
12+
@BeforeEach
13+
public void setup() {
1414
solution1 = new _1062.Solution1();
1515
}
1616

0 commit comments

Comments
 (0)