Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
Given a binary string s and an integer k, return true if every binary code of length k is a substring of s. Otherwise, return false.
Example 1:
Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indices 0, 1, 3 and 2 respectively.
Example 2:
Input: s = "0110", k = 1 Output: true Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
Example 3:
Input: s = "0110", k = 2 Output: false Explanation: The binary code "00" is of length 2 and does not exist in the array.
Constraints:
1 <= s.length <= 5 * 105s[i] is either '0' or '1'.1 <= k <= 20Problem summary: Given a binary string s and an integer k, return true if every binary code of length k is a substring of s. Otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation
"00110110" 2
"0110" 1
"0110" 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1461: Check If a String Contains All Binary Codes of Size K
class Solution {
public boolean hasAllCodes(String s, int k) {
int n = s.length();
int m = 1 << k;
if (n - k + 1 < m) {
return false;
}
Set<String> ss = new HashSet<>();
for (int i = 0; i < n - k + 1; ++i) {
ss.add(s.substring(i, i + k));
}
return ss.size() == m;
}
}
// Accepted solution for LeetCode #1461: Check If a String Contains All Binary Codes of Size K
func hasAllCodes(s string, k int) bool {
n, m := len(s), 1<<k
if n-k+1 < m {
return false
}
ss := map[string]bool{}
for i := 0; i+k <= n; i++ {
ss[s[i:i+k]] = true
}
return len(ss) == m
}
# Accepted solution for LeetCode #1461: Check If a String Contains All Binary Codes of Size K
class Solution:
def hasAllCodes(self, s: str, k: int) -> bool:
n = len(s)
m = 1 << k
if n - k + 1 < m:
return False
ss = {s[i : i + k] for i in range(n - k + 1)}
return len(ss) == m
// Accepted solution for LeetCode #1461: Check If a String Contains All Binary Codes of Size K
struct Solution;
impl Solution {
fn has_all_codes(s: String, k: i32) -> bool {
let k = k as usize;
let m = 1 << k;
let mut set = vec![false; m];
let mut x = 0;
let n = s.len();
let mask = m as u32 - 1;
for (i, c) in s.char_indices().rev() {
x <<= 1;
x |= (c as u8 - b'0') as u32;
x &= mask;
if i + k <= n {
set[x as usize] = true;
}
}
set.iter().all(|&b| b)
}
}
#[test]
fn test() {
let s = "00110110".to_string();
let k = 2;
let res = true;
assert_eq!(Solution::has_all_codes(s, k), res);
let s = "00110".to_string();
let k = 2;
let res = true;
assert_eq!(Solution::has_all_codes(s, k), res);
let s = "0110".to_string();
let k = 1;
let res = true;
assert_eq!(Solution::has_all_codes(s, k), res);
let s = "0110".to_string();
let k = 2;
let res = false;
assert_eq!(Solution::has_all_codes(s, k), res);
let s = "0000000001011100".to_string();
let k = 4;
let res = false;
assert_eq!(Solution::has_all_codes(s, k), res);
}
// Accepted solution for LeetCode #1461: Check If a String Contains All Binary Codes of Size K
function hasAllCodes(s: string, k: number): boolean {
const n = s.length;
const m = 1 << k;
if (n - k + 1 < m) {
return false;
}
const ss = new Set<string>();
for (let i = 0; i + k <= n; ++i) {
ss.add(s.slice(i, i + k));
}
return ss.size === m;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.