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 <= 1000s[i] is either '0' or '1'.1 <= n <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute-force approach means checking if our big string contains the binary representation of every number from 1 to our target number N. We will generate the binary representation for each number and then check to see if it is a substring of the given string.
Here's how the algorithm would work step-by-step:
def string_contains_binary_numbers(
big_string: str, target_number: int) -> bool:
for current_number in range(1, target_number + 1):
# Convert the current number to its binary string representation
binary_representation =
bin(current_number)[2:]
# Check if the binary string is a substring of the big string
if binary_representation not in big_string:
# If not a substring, then we can stop and return false
return False
# If all binary representations were found, return true
return TrueThe core idea is to check if all the binary representations of numbers from 1 to N are present as substrings within the given binary string. We can efficiently accomplish this by systematically generating all such binary representations and verifying their presence. This avoids complex search algorithms.
Here's how the algorithm would work step-by-step:
def string_contains_binary_codes(binary_string, number):
for integer in range(1, number + 1):
binary_representation = bin(integer)[2:]
# Check if the binary representation is a substring.
if binary_representation not in binary_string:
return False
return True
def is_substring(main_string, sub_string):
#Check if substring is within mainstring
return sub_string in main_string
def binary_string_contains_substrings(binary_string, number):
# Iterate from 1 to the input number.
for integer in range(1, number + 1):
# Convert the integer to its binary string representation.
binary_representation = bin(integer)[2:]
# Check if the binary representation is a substring of the main string.
# If not, return False immediately.
if not is_substring(binary_string, binary_representation):
return False
# If all binary representations are found, return True.
return True
def check_binary_string_contains_substrings(binary_string, number):
# Iterate through numbers to check their binary representations
for integer in range(1, number + 1):
binary_representation = bin(integer)[2:]
# This is the core logic to determine if each binary
# representation is a substring within the binary string.
if binary_representation not in binary_string:
return False
# All binary representations were found.
return True| Case | How to Handle |
|---|---|
| Empty string s or n = 0 | Return true immediately because any substring can be found in an empty string if n is zero |
| n is larger than the maximum possible number representable by a substring of s given s's length | Calculate the maximum possible number for a given substring length in s and return false if n is bigger than that |
| s is shorter than the minimum length required to contain substrings representing 1 to n | Return false because the string can't possibly contain all required substrings. |
| n is a very large number causing possible integer overflow if not handled correctly | Use long or appropriate data type to prevent overflow while checking substrings |
| s contains characters other than '0' and '1' | Throw an IllegalArgumentException or return false because the input string is invalid |
| s consists only of '0' characters and n is not 0 | Return false because the string can only represent 0 and nothing higher than 0 |
| n = 1 and s is an empty string | Return false since even representing '1' is impossible in an empty string |
| The solution is time-complexity inefficient and results in Time Limit Exceeded for large s and n | Optimize the substring search or use a rolling hash to improve the performance |