-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path187-repeated-dna-sequences.js
49 lines (47 loc) · 1.04 KB
/
187-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
/**
* @param {string} s
* @return {string[]}
*/
const findRepeatedDnaSequences = function(s) {
if (!s || s.length < 10) {
return [];
}
const map = new Map([["A", 0], ["C", 1], ["G", 2], ["T", 3]]);
const dna = new Set();
const repeated = new Set();
const mask = 0xfffff;
let cur = 0;
for (let i = 0, n = s.length; i < n; i++) {
cur <<= 2;
cur = cur | map.get(s[i]);
cur = cur & mask;
if (i >= 9) {
if (dna.has(cur)) {
const seq = s.slice(i - 9, i + 1);
if (!repeated.has(seq)) {
repeated.add(seq);
}
} else {
dna.add(cur);
}
}
}
return Array.from(repeated);
};
// another
/**
* @param {string} s
* @return {string[]}
*/
const findRepeatedDnaSequences = function(s) {
let store = new Set(), result = new Set()
for(let i = 0; i < s.length - 9; i++) {
const str = s.substring(i, i + 10)
if(store.has(str)) {
result.add(str)
} else {
store.add(str)
}
}
return Array.from(result)
};