Binary String With Substrings Representing 1 To N - Problem
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.
Input & Output
Example 1 — All Found
$
Input:
s = "0110", n = 3
›
Output:
true
💡 Note:
Binary representations: 1="1", 2="10", 3="11". All are substrings of "0110": "1" at index 1, "10" at index 1, "11" at index 2.
Example 2 — Missing Number
$
Input:
s = "0110", n = 4
›
Output:
false
💡 Note:
Binary of 4 is "100". The string "0110" does not contain "100" as a substring, so return false.
Example 3 — Single Digit
$
Input:
s = "1", n = 1
›
Output:
true
💡 Note:
Only need to check if binary of 1 ("1") exists in "1", which it does.
Constraints
- 1 ≤ s.length ≤ 1000
- s[i] is either '0' or '1'
- 1 ≤ n ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Binary string s and integer n
2
Process
Check each number's binary form exists in s
3
Output
Return true if all found, false otherwise
Key Takeaway
🎯 Key Insight: Convert each number to binary and check substring existence - larger numbers are more restrictive
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code