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) {
2828function getSubSequance ( s , i , len ) {
2929return 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+ } ;