forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0187-repeated-dna-sequences.js
55 lines (48 loc) · 1.45 KB
/
0187-repeated-dna-sequences.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* https://leetcode.com/problems/repeated-dna-sequences/
* Hashing
* s = the number of letters in the sequence. In our case, it's 10. so the time complexity would be 10*n which boils down to n.
* Time O(n) | Space O(n)
* @param {string} s
* @return {string[]}
*/
var findRepeatedDnaSequences = function (s) {
const sequenceSet = new Set();
let resultSet = new Set();
for (let i = 0; i < s.length; i++) {
const subSequence = getSubSequence(s, i, 10);
if (sequenceSet.has(subSequence)) {
resultSet.add(subSequence);
} else {
sequenceSet.add(subSequence);
}
}
resultSet = [...resultSet];
return resultSet;
};
function getSubSequence(s, i, len) {
return s.slice(i, i + len);
}
// an alternative code with the same approach.
/**
* https://leetcode.com/problems/repeated-dna-sequences/
* Hashing
* s = the number of letters in the sequence. In our case, it's 10. so the time complexity would be 10*n which boils down to n.
* Time O(n) | Space O(n)
* @param {string} s
* @return {string[]}
*/
var findRepeatedDnaSequences1 = function (s) {
const seen = new Set();
const res = new Set();
const arr = Array.from(s);
for (let l = 0; l < arr.length - 9; l++) {
const sequence = s.slice(l, l + 10);
if (seen.has(sequence)) {
res.add(sequence);
} else {
seen.add(sequence);
}
}
return Array.from(res);
};