forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeated-dna-sequences.js
More file actions
37 lines (31 loc) · 875 Bytes
/
repeated-dna-sequences.js
File metadata and controls
37 lines (31 loc) · 875 Bytes
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
/**
* Repeated DNA Sequences
*
* All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG".
* When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
*
* Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
*
* Example:
*
* Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
*
* Output: ["AAAAACCCCC", "CCCCCAAAAA"]
*/
/**
* @param {string} s
* @return {string[]}
*/
const findRepeatedDnaSequences = s => {
const seen = new Set();
const repeated = new Set();
for (let i = 0; i + 9 < s.length; i++) {
const sequence = s.substring(i, i + 10);
if (seen.has(sequence)) {
repeated.add(sequence);
}
seen.add(sequence);
}
return Array.from(repeated);
};
export { findRepeatedDnaSequences };