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 <= 1000
s[i]
is either '0'
or '1'
.1 <= n <= 109
This problem asks whether all binary representations of numbers from 1 to n
are substrings of a given binary string s
. The naive approach of checking all numbers from 1 to n
is inefficient for large n
. The key optimization lies in recognizing that if a number's binary representation is not found, then neither will be any of its multiples.
Optimized Approach:
The solution iterates downwards from n
to n/2 + 1
. This is because if the binary representation of a number i
(where i > n/2
) is not present in s
, then it is guaranteed that no multiples of i
(which are necessarily larger than n
) are present. Checking only this range drastically reduces the number of checks needed.
Time Complexity Analysis:
n/2
times.Integer.toBinaryString(i)
(or its equivalent in other languages) takes O(log i) time, where i
is at most n
.s.contains()
(or equivalent string search) takes O(len(s)) in the worst case.Therefore, the overall time complexity is approximately O(n/2 * (log n + len(s))), which can be simplified to O(n log n + n * len(s)). In the constraints, len(s)
is at most 1000, which is a constant. Therefore, the complexity becomes essentially O(n log n), dominated by the number of iterations and binary conversion.
Space Complexity Analysis:
The space complexity depends mainly on the space used for storing the binary representation of i
. This is O(log n) at most. Therefore, the overall space complexity is O(log n), which is relatively small.
Code Explanation (Python):
class Solution:
def queryString(self, s: str, n: int) -> bool:
if n > 1000: # Optimization: Prevents unnecessary computations for very large n
return False
return all(bin(i)[2:] in s for i in range(n, n // 2, -1))
The Python code elegantly uses the all()
function combined with a generator expression. It checks if all binary representations from n
down to n//2 +1
are present as substrings in s
. The bin(i)[2:]
slice removes the "0b" prefix from the binary string.
Code Explanation (Java):
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;
}
}
The Java code is a straightforward implementation of the algorithm. It iterates through the numbers, converts them to binary strings using Integer.toBinaryString()
, and checks if each binary string is a substring of s
using s.contains()
. The early exit (return false
) when a substring is not found is crucial for efficiency.
The C++, Go, and TypeScript solutions follow similar logic, adapting the string manipulation and substring checks to their respective language features. They all share the same optimized time complexity of approximately O(n log n).