Skip to content

Commit 62ef8fb

Browse files
authored
Update 1062-longest-repeating-substring.js
1 parent 162c1fc commit 62ef8fb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

1062-longest-repeating-substring.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const longestRepeatingSubstring = function(s) {
6+
let left = 0;
7+
let right = s.length - 1;
8+
while(left < right) {
9+
let pivot = Math.floor((left + right + 1) / 2);
10+
if (hasRepeat(s, pivot)) {
11+
left = pivot;
12+
} else {
13+
right = pivot - 1;
14+
}
15+
}
16+
return left;
17+
};
18+
19+
const hasRepeat = (s, l) => {
20+
const strings = new Set();
21+
for (let i = 0; i < s.length - l + 1; i++) {
22+
const sub = s.substr(i, l);
23+
if (strings.has(sub)) {
24+
return true;
25+
}
26+
strings.add(sub);
27+
}
28+
return false;
29+
}
30+
31+
// another
32+
133
/**
234
* @param {string} s
335
* @return {number}

0 commit comments

Comments
 (0)