Skip to content

Commit 72fbe47

Browse files
committed
Add solution #1461
1 parent 3b48110 commit 72fbe47

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,313 LeetCode solutions in JavaScript
1+
# 1,314 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1116,6 +1116,7 @@
11161116
1457|[Pseudo-Palindromic Paths in a Binary Tree](./solutions/1457-pseudo-palindromic-paths-in-a-binary-tree.js)|Medium|
11171117
1458|[Max Dot Product of Two Subsequences](./solutions/1458-max-dot-product-of-two-subsequences.js)|Hard|
11181118
1460|[Make Two Arrays Equal by Reversing Sub-arrays](./solutions/1460-make-two-arrays-equal-by-reversing-sub-arrays.js)|Easy|
1119+
1461|[Check If a String Contains All Binary Codes of Size K](./solutions/1461-check-if-a-string-contains-all-binary-codes-of-size-k.js)|Medium|
11191120
1462|[Course Schedule IV](./solutions/1462-course-schedule-iv.js)|Medium|
11201121
1464|[Maximum Product of Two Elements in an Array](./solutions/1464-maximum-product-of-two-elements-in-an-array.js)|Easy|
11211122
1466|[Reorder Routes to Make All Paths Lead to the City Zero](./solutions/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.js)|Medium|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 1461. Check If a String Contains All Binary Codes of Size K
3+
* https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary string s and an integer k, return true if every binary code of length k is a
7+
* substring of s. Otherwise, return false.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @param {number} k
13+
* @return {boolean}
14+
*/
15+
var hasAllCodes = function(s, k) {
16+
const requiredCount = 1 << k;
17+
const seenCodes = new Set();
18+
19+
for (let i = 0; i <= s.length - k; i++) {
20+
seenCodes.add(s.slice(i, i + k));
21+
if (seenCodes.size === requiredCount) return true;
22+
}
23+
24+
return false;
25+
};

0 commit comments

Comments
 (0)