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
Binary String Substring Problem: s="0110", n=3Input String s"0110"n = 3Check 1,2,31→"1"2→"10"3→"11""0110" contains "1" ✓ "10" ✓ "11" ✓Result: true
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
Asked in
Google 35 Facebook 28
32.0K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen