You are given a 0-indexed binary string s which represents the types of buildings along a street where:
s[i] = '0' denotes that the ith building is an office ands[i] = '1' denotes that the ith building is a restaurant.As a city official, you would like to select 3 buildings for random inspection. However, to ensure variety, no two consecutive buildings out of the selected buildings can be of the same type.
s = "001101", we cannot select the 1st, 3rd, and 5th buildings as that would form "011" which is not allowed due to having two consecutive buildings of the same type.Return the number of valid ways to select 3 buildings.
Example 1:
Input: s = "001101" Output: 6 Explanation: The following sets of indices selected are valid: - [0,2,4] from "001101" forms "010" - [0,3,4] from "001101" forms "010" - [1,2,4] from "001101" forms "010" - [1,3,4] from "001101" forms "010" - [2,4,5] from "001101" forms "101" - [3,4,5] from "001101" forms "101" No other selection is valid. Thus, there are 6 total ways.
Example 2:
Input: s = "11100" Output: 0 Explanation: It can be shown that there are no valid selections.
Constraints:
3 <= s.length <= 105s[i] is either '0' or '1'.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:
The brute force approach for counting building selections involves checking every single possible combination of buildings. We consider each building individually and decide whether to include it or not in our selection. By exhaustively exploring all combinations, we can count the valid ones based on the given selection rules.
Here's how the algorithm would work step-by-step:
def count_building_selections_brute_force(buildings):
number_of_buildings = len(buildings)
number_of_valid_selections = 0
# Iterate through all possible subsets using binary representation
for i in range(2**number_of_buildings):
current_selection = []
# Construct the current selection based on the bits of i
for building_index in range(number_of_buildings):
if (i >> building_index) & 1:
current_selection.append(buildings[building_index])
is_valid = True
# Ensure we do not access an invalid index
if len(current_selection) > 1:
for building_index in range(len(current_selection) - 1):
# Check for adjacent buildings of same type
if current_selection[building_index] == current_selection[building_index + 1]:
is_valid = False
break
# Increment valid selections count, if valid
if is_valid:
number_of_valid_selections += 1
return number_of_valid_selectionsThe key to efficiently counting building arrangements is to avoid checking every possible combination. Instead, we focus on building valid arrangements incrementally by considering each building type (office or apartment) one at a time and leveraging information about previously built sections.
Here's how the algorithm would work step-by-step:
def number_of_ways_to_select_buildings(street):
one_building_sequences = 0
two_building_sequences = 0
three_building_sequences = 0
for building in street:
if building == '0': # Current building is an office
three_building_sequences += two_building_sequences
# Adding to single building sequences.
one_building_sequences += 1
two_building_sequences += one_building_sequences - 1
else: # Current building is an apartment building
three_building_sequences += two_building_sequences
one_building_sequences += 1
# Increment two building sequences, using the previous count
two_building_sequences += one_building_sequences - 1
return three_building_sequences| Case | How to Handle |
|---|---|
| Null or empty input string | Return 0 if the input string is null or empty as there are no buildings to select. |
| Input string with length less than 3 | Return 0 as at least three characters are needed to form a valid building sequence. |
| Input string with only '0's | The '1' count will be zero leading to a zero product, thus 0 valid building sequences. |
| Input string with only '1's | The '0' count will be zero leading to a zero product, thus 0 valid building sequences. |
| Large input string exceeding memory constraints | Optimize the solution to use constant space, avoiding storing large intermediate results. |
| Input string contains characters other than '0' and '1' | Throw an IllegalArgumentException or return an error code to indicate invalid input. |
| Input string with alternating '0' and '1' characters (e.g., '010101') | The counts of '0' and '1' will be approximately equal, potentially maximizing the number of valid sequences, ensure multiplication of the counts doesn't lead to integer overflow. |
| Input string with a skewed distribution of '0's and '1's (e.g., many '0's followed by a few '1's) | The number of possible selections will depend on the scarcity of one of the digits, so the algorithm should handle cases where one digit is rare. |