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 a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "0110", n = 3 Output: true
Example 2:
Input: s = "0110", n = 4 Output: false
Constraints:
1 <= s.length <= 1000s[i] is either '0' or '1'.1 <= n <= 109Problem summary: Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise. A substring is a contiguous sequence of characters within a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation · Sliding Window
"0110" 3
"0110" 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1016: Binary String With Substrings Representing 1 To N
class Solution {
public boolean queryString(String s, int n) {
if (n > 1000) {
return false;
}
for (int i = n; i > n / 2; i--) {
if (!s.contains(Integer.toBinaryString(i))) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #1016: Binary String With Substrings Representing 1 To N
func queryString(s string, n int) bool {
if n > 1000 {
return false
}
for i := n; i > n/2; i-- {
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
return false
}
}
return true
}
# Accepted solution for LeetCode #1016: Binary String With Substrings Representing 1 To N
class Solution:
def queryString(self, s: str, n: int) -> bool:
if n > 1000:
return False
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))
// Accepted solution for LeetCode #1016: Binary String With Substrings Representing 1 To N
impl Solution {
pub fn query_string(s: String, n: i32) -> bool {
if n > 1000 {
return false;
}
for i in (n / 2 + 1..=n).rev() {
if !s.contains(&format!("{:b}", i)) {
return false;
}
}
true
}
}
// Accepted solution for LeetCode #1016: Binary String With Substrings Representing 1 To N
function queryString(s: string, n: number): boolean {
if (n > 1000) {
return false;
}
for (let i = n; i > n / 2; --i) {
if (s.indexOf(i.toString(2)) === -1) {
return false;
}
}
return true;
}
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.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.