Skip to content

Commit 3c1231a

Browse files
authored
Update 0187-repeated-dna-sequences.js
fixing conflict. adding alternative code.
1 parent 047feab commit 3c1231a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Diff for: javascript/0187-repeated-dna-sequences.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* https://leetcode.com/problems/repeated-dna-sequences/
33
* Hashing
44
* s = the number of letters in the sequance. In our case it's 10. so the time complexity would be 10*n which boils down to n.
5-
* Time O(s*n) | Space O(n)
5+
* Time O(n) | Space O(n)
66
* @param {string} s
77
* @return {string[]}
88
*/
@@ -28,4 +28,30 @@ var findRepeatedDnaSequences = function(s) {
2828
function getSubSequance(s,i,len) {
2929
return s.slice(i, i + len);
3030
}
31-
31+
32+
// an alternative code with the same approch.
33+
/**
34+
* https://leetcode.com/problems/repeated-dna-sequences/
35+
* Hashing
36+
* s = the number of letters in the sequance. In our case it's 10. so the time complexity would be 10*n which boils down to n.
37+
* Time O(n) | Space O(n)
38+
* @param {string} s
39+
* @return {string[]}
40+
*/
41+
var findRepeatedDnaSequences1 = function (s) {
42+
const seen = new Set();
43+
const res = new Set();
44+
const arr = Array.from(s);
45+
46+
for (let l = 0; l < arr.length - 9; l++) {
47+
const sequence = s.slice(l, l + 10);
48+
49+
if (seen.has(sequence)) {
50+
res.add(sequence);
51+
} else {
52+
seen.add(sequence);
53+
}
54+
}
55+
56+
return Array.from(res);
57+
};

0 commit comments

Comments
 (0)