You are given a binary string binary. A subsequence of binary is considered good if it is not empty and has no leading zeros (with the exception of "0").
Find the number of unique good subsequences of binary.
binary = "001", then all the good subsequences are ["0", "0", "1"], so the unique good subsequences are "0" and "1". Note that subsequences "00", "01", and "001" are not good because they have leading zeros.Return the number of unique good subsequences of binary. Since the answer may be very large, return it modulo 109 + 7.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: binary = "001" Output: 2 Explanation: The good subsequences of binary are ["0", "0", "1"]. The unique good subsequences are "0" and "1".
Example 2:
Input: binary = "11" Output: 2 Explanation: The good subsequences of binary are ["1", "1", "11"]. The unique good subsequences are "1" and "11".
Example 3:
Input: binary = "101" Output: 5 Explanation: The good subsequences of binary are ["1", "0", "1", "10", "11", "101"]. The unique good subsequences are "0", "1", "10", "11", and "101".
Constraints:
1 <= binary.length <= 105binary consists of only '0's and '1's.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 involves generating every single possible subsequence from the given sequence. Then, we check if each subsequence is "good" based on the problem's definition. Finally, we count only the unique "good" subsequences.
Here's how the algorithm would work step-by-step:
def number_of_unique_good_subsequences_brute_force(sequence):
all_subsequences = set()
def generate_subsequences(index, current_subsequence):
# Base case: when we have processed all elements
if index == len(sequence):
if current_subsequence:
all_subsequences.add(tuple(current_subsequence))
return
# Exclude the current element
generate_subsequences(index + 1, current_subsequence)
# Include the current element
generate_subsequences(index + 1, current_subsequence + [sequence[index]])
generate_subsequences(0, [])
good_subsequences = set()
# Iterate through each subsequence
for subsequence_tuple in all_subsequences:
subsequence = list(subsequence_tuple)
is_good = True
# A sequence is not good if it starts with zero
if subsequence[0] == 0:
is_good = False
if is_good:
good_subsequences.add(tuple(subsequence))
# We must consider the '0' case as a valid sequence.
number_unique_good_subsequences = len(good_subsequences)
# Add 1 if the original sequence contained zero.
if 0 in sequence:
number_unique_good_subsequences += 1
return number_unique_good_subsequencesThe goal is to count special sequences of digits within a given sequence, considering that a special sequence must only contain '1's and '0's, and must have a unique value. To avoid recounting sequences that represent the same value, we'll build the count gradually by including each digit and updating a running total of the number of unique values we've seen.
Here's how the algorithm would work step-by-step:
def number_of_unique_good_subsequences(binary_string):
modulo = 1000000007
unique_sequences_count = 0
seen_zero = False
for digit in binary_string:
if digit == '1':
# Doubling the count and adding 1 for '1'
unique_sequences_count = (unique_sequences_count * 2 + 1) % modulo
elif digit == '0':
#Doubling the count for '0'
unique_sequences_count = (unique_sequences_count * 2) % modulo
# Only add 1 for '0' if it's the first '0' we've seen.
if not seen_zero:
unique_sequences_count = (unique_sequences_count + 1) % modulo
seen_zero = True
return unique_sequences_count| Case | How to Handle |
|---|---|
| Null or empty input string | Return 0 since there are no subsequences possible. |
| String containing only '0's | Return 1, as '0' is the only valid subsequence. |
| String containing only '1's | Return 1, as '1' is the only valid subsequence. |
| String with length 1 | Return 1 if the single character is valid (either '0' or '1'), otherwise return 0. |
| String with leading zeros | The dynamic programming solution will correctly consider subsequences starting with '0'. |
| Very long string (potential for integer overflow) | Use modulo operator (%) with a prime number during calculation to prevent integer overflow. |
| String with repeating subsequences | The dynamic programming approach ensures unique subsequences are counted. |
| String contains both '0' and '1' | The dynamic programming solution accounts for both '0' and '1' and their possible combinations. |