Binary String With Substrings Representing 1 To N - Problem
Binary String With Substrings Representing 1 To N

You are given a binary string s and a positive integer n. Your task is to determine whether the binary representation of all integers from 1 to n can be found as substrings within s.

For example, if n = 3, you need to check if the binary representations of 1 ("1"), 2 ("10"), and 3 ("11") all exist as contiguous substrings in s.

Goal: Return true if all binary representations from 1 to n are substrings of s, otherwise return false.

A substring is a contiguous sequence of characters within a string. This problem tests your understanding of string manipulation, binary representations, and efficient searching techniques.

Input & Output

example_1.py โ€” Simple Case
$ Input: s = "0110", n = 3
โ€บ Output: true
๐Ÿ’ก Note: We need binary representations: 1โ†’"1", 2โ†’"10", 3โ†’"11". All can be found in "0110": "1" at index 1, "10" at index 0-1, "11" at index 1-2.
example_2.py โ€” Missing Binary
$ Input: s = "0110", n = 4
โ€บ Output: false
๐Ÿ’ก Note: We need: 1โ†’"1", 2โ†’"10", 3โ†’"11", 4โ†’"100". The binary "100" cannot be found as a substring in "0110".
example_3.py โ€” Large n Edge Case
$ Input: s = "1", n = 1000000
โ€บ Output: false
๐Ÿ’ก Note: With string length 1, it's impossible to contain binary representations of all numbers up to 1,000,000. Early termination optimization catches this.

Visualization

Tap to expand
๐Ÿ” Binary Substring DetectiveEvidence String: "110101111000"Need to find all binary clues for numbers 1 through nClue #1: "1"โœ“Clue #2: "10"โœ“Clue #3: "11"โœ“Clue #4: "100"โœ—๐Ÿš€ Detective's ShortcutIf n is very large, some binary clues would be longerthan the evidence string itself - case closed early!Example: n=1000000 needs 20+ bit clues, impossible in short stringsโŒ Case Status: UNSOLVEDMissing clue "100" - cannot proceed
Understanding the Visualization
1
List Required Evidence
Convert each number 1 to n into its binary 'fingerprint'
2
Search the Scene
Look for each binary fingerprint in the evidence string
3
Early Elimination
If n is too large, some fingerprints would be longer than the evidence itself
4
Case Conclusion
Return true only if ALL binary fingerprints are found
Key Takeaway
๐ŸŽฏ Key Insight: The exponential growth of binary representations allows for powerful early termination - if n requires binary strings longer than our evidence string, the case is impossible to solve!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(min(n * m * log n, 1))

O(1) for large n due to early termination, otherwise same as brute force

n
2n
โšก Linearithmic
Space Complexity
O(log n)

Space for binary representation storage

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s[i] is either '0' or '1'
  • 1 โ‰ค n โ‰ค 109
  • Note: For very large n, the problem becomes impossible due to binary length constraints
Asked in
Google 25 Facebook 15 Amazon 12 Microsoft 8
28.4K Views
Medium Frequency
~15 min Avg. Time
892 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