Meta Data#
Difficulty: medium First Attempt: 2026-01-04
- Submissions: Memory Limit Exceeded → Wrong Answer ×4 → Accepted
Approach#
n can reach 10^9, but s has at most 1000 characters.
Key observation: every number in (n/2, n] has the same binary length L, and there are about n/2 of them. s has at most len(s) substrings of length L, so whenever n/2 > len(s) some number is missing. When the answer is True, n must be small (about <= 2000).
Two approaches:
- Check downward from
n: test each number withbin(i)[2:] in s. For largenthe first few checks fail; for smallnthere are only a few thousand checks. - Collect with a sliding window: from each
1ins, extend right while computing the binary value, stopping pastnor 30 bits; put every value<= ninto asetand check whether its size isn.
My Attempts#
| Version | Problem |
|---|---|
| Attempt 1, MLE | Put the binary strings of 1..n into a set first; with n = 10^9 memory ran out |
| Attempt 2, WA | Switched to “enough distinct substrings of each length”, requiring both "0" and "1" for length 1. But 0 is not in 1..n. For s = "1", n = 1, the answer is True; this returned False |
| Attempts 3–5, WA | Required 2^w - 1 kinds of length w. But substrings can have leading zeros ("0", "01"), and n need not use every length-w number, so the right count does not mean the right contents. For s = "0", n = 1, length 1 has one kind, but it is "0", not "1"; the answer is False and this returned True |
| AC | Checked every number from n downward with in |
Solution#
class Solution:
def queryString(self, s: str, n: int) -> bool:
for i in range(n, 0, -1): # large numbers first; a big n fails fast
if bin(i)[2:] not in s:
return False
return True
Sliding-window collection version:
class Solution:
def queryString(self, s: str, n: int) -> bool:
seen = set()
for i in range(len(s)):
if s[i] == "0":
continue # binary representations have no leading 0
val = 0
for j in range(i, min(len(s), i + 30)):
val = val * 2 + (s[j] == "1")
if val > n:
break
seen.add(val)
return len(seen) == n
Complexity#
- Downward check: when the answer is
True,n = O(len(s)), and eachinisO(len(s) · log n), aboutO(len(s)^2 · log n)total; for largenit fails within the first few numbers. - Collection: time
O(30 · len(s)), spaceO(min(n, 30 · len(s))).
Mistakes to Avoid#
- Storing all of
1..n:ncan be10^9. - Judging by the number of distinct substrings: leading zeros and values outside
1..nget mixed in.
Interview Explanation#
s has at most 1000 characters, and all numbers between n/2 and n share one binary length, so for n above roughly twice len(s) the answer must be False. I can check from n downward whether each number is a substring, which fails quickly for large n; or extend up to 30 bits from each 1, collect every value <= n in a set, and compare its size with n, in O(30 · len(s)).
