Skip to content

Commit fff4dab

Browse files
authored
Create 1461-check-if-a-string-contains-all-binary-codes-of-size-k.js
Added a solution for check-if-a-string-contains-all-binary-codes-of-size-k in JS.
1 parent 9d668c6 commit fff4dab

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/
3+
*
4+
* Hashing
5+
* Time O(n*k) | Space O(2^k) (it can't get any bigger than 2^k in the worst case)
6+
* @param {string} s
7+
* @param {number} k
8+
* @return {boolean}
9+
*/
10+
var hasAllCodes = function(s, k) {
11+
12+
const bitSet = new Set();
13+
14+
for(let i = 0; i < s.length; i++) {
15+
if(s.substring(i,i+k).length === k) {
16+
bitSet.add(s.substring(i,i + k));
17+
}
18+
}
19+
20+
return bitSet.size === 1<<k;
21+
};
22+

0 commit comments

Comments
 (0)