You are given a positive integer n
.
A binary string x
is valid if all substrings of x
of length 2 contain at least one "1"
.
Return all valid strings with length n
, in any order.
Example 1:
Input: n = 3
Output: ["010","011","101","110","111"]
Example 2:
Input: n = 1
Output: ["0","1"]
Explain how you would generate all valid binary strings with a length of n
. Explain your approach, time complexity, and space complexity.
When 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:
We need to create binary strings (strings made of 0s and 1s) of a specific length, but no two 0s can be next to each other. The brute force way is to try absolutely every possible combination of 0s and 1s of that length, and then check if each one follows the rule.
Here's how the algorithm would work step-by-step:
def generate_binary_strings_brute_force(string_length):
valid_strings = []
number_of_possible_strings = 2 ** string_length
for i in range(number_of_possible_strings):
# Convert the integer to its binary representation
binary_string = bin(i)[2:].zfill(string_length)
# Check for adjacent zeros
if not has_adjacent_zeros(binary_string):
#If no adjacent zeros, add to result
valid_strings.append(binary_string)
return valid_strings
def has_adjacent_zeros(binary_string):
# Iterate through the string and check for adjacent 0s
for i in range(len(binary_string) - 1):
#This is the crucial step of the logic
if binary_string[i] == '0' and binary_string[i+1] == '0':
return True
return False
We'll construct our binary strings piece by piece, ensuring no adjacent zeros appear. The core idea is to keep track of the last digit we added and intelligently choose the next one, making sure we don't break the rule.
Here's how the algorithm would work step-by-step:
def generate_binary_strings_without_adjacent_zeros(string_length):
result = []
def generate(current_string, last_digit):
if len(current_string) == string_length:
result.append(current_string)
return
# If the last digit was '1', we can add '0' or '1'
if last_digit == '1' or last_digit is None:
generate(current_string + '0', '0')
generate(current_string + '1', '1')
# If the last digit was '0', we must add '1'
elif last_digit == '0':
generate(current_string + '1', '1')
# Start generation with both '0' and '1' as initial digits.
generate('0', '0')
generate('1', '1')
return result
Case | How to Handle |
---|---|
Input n is 0 | Return an empty string list as there are no binary strings of length 0. |
Input n is 1 | Return a list containing '0' and '1' as these are the only valid strings. |
Large n value | Be mindful of exponential growth in the number of strings generated and potential memory constraints if storing all strings. |
n is a negative integer | Throw an IllegalArgumentException or return an empty list, indicating invalid input. |
Recursive solution stack overflow with very large n | Consider using an iterative approach to avoid stack overflow for large inputs. |
Memory limits exceeded when storing intermediate results for larger n. | Consider printing or streaming the results instead of storing them all in memory. |
All binary strings starting with '0' will have limited application | Consider an option to filter the results further when applicable. |
Outputting strings vs numbers. | Ensure your numbers do not have leading zeroes in the output or in the strings generated if you are converting from numerical representation, as '01' has no mathematical meaning when representing a number. |