File tree 1 file changed +28
-2
lines changed
1 file changed +28
-2
lines changed Original file line number Diff line number Diff line change 2
2
* https://leetcode.com/problems/repeated-dna-sequences/
3
3
* Hashing
4
4
* 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)
6
6
* @param {string } s
7
7
* @return {string[] }
8
8
*/
@@ -28,4 +28,30 @@ var findRepeatedDnaSequences = function(s) {
28
28
function getSubSequance ( s , i , len ) {
29
29
return s . slice ( i , i + len ) ;
30
30
}
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
+ } ;
You can’t perform that action at this time.
0 commit comments