Skip to content

Commit b1155a8

Browse files
authoredJul 7, 2021
Update 1044-longest-duplicate-substring.js
1 parent a428617 commit b1155a8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
 

‎1044-longest-duplicate-substring.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const longestDupSubstring = function(s) {
6+
const n = s.length
7+
let l = 0, r = n, res = ''
8+
while(l < r) {
9+
const mid = (l + r + 1) >> 1
10+
const [chk, str] = valid(s, mid)
11+
if(chk) {
12+
l = mid
13+
res = str
14+
} else {
15+
r = mid - 1
16+
}
17+
}
18+
return res
19+
};
20+
21+
function valid(s, len) {
22+
const set = new Set()
23+
for(let i = 0, n = s.length; i <= n - len; i++) {
24+
const tmp = s.substr(i, len)
25+
if(set.has(tmp)) return [true, tmp]
26+
set.add(tmp)
27+
}
28+
29+
return [false, '']
30+
}
31+
32+
// another
33+
134
/**
235
* @param {string} S
336
* @return {string}

0 commit comments

Comments
 (0)
Please sign in to comment.