You are given a binary string s. A subsequence of s is considered good if it is not empty and has a value that is a power of 5.
s = "1011", then "1", "11", "101", and "1011" are some of its subsequences. Among these, only the subsequence "101" has a value of 5, which is a power of 5.Return the number of good subsequences in s. Since the answer may be large, return it modulo 109 + 7.
s = "101", then its value is 5.Example 1:
Input: s = "000" Output: 0 Explanation: There are no good subsequences, so we return 0.
Example 2:
Input: s = "1011" Output: 5 Explanation: The good subsequences are "1", "1", "101", "11", and "1011".
Example 3:
Input: s = "0110" Output: 2 Explanation: The good subsequences are "10" and "10".
Constraints:
1 <= s.length <= 104s[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:
To solve this problem using the brute force approach, we look at every single possible sub-piece of the given word and test whether it qualifies as a good sub-piece. A good sub-piece is defined as one where all of its unique characters appear the exact same number of times.
Here's how the algorithm would work step-by-step:
class Solution:
def countGoodSubsequences(self, word: str) -> int:
from itertools import combinations
from collections import Counter
total_good_count = 0
modulo_value = 10**9 + 7
seen_subsequences = set()
# Generate every possible subsequence length to check all combinations
for subsequence_length in range(1, len(word) + 1):
for index_combination in combinations(range(len(word)), subsequence_length):
subsequence_string = ''.join(word[index_identifier] for index_identifier in index_combination)
if subsequence_string in seen_subsequences:
continue
seen_subsequences.add(subsequence_string)
character_frequencies = Counter(subsequence_string)
frequency_values = list(character_frequencies.values())
# Verify if all unique characters appear the exact same number of times
if len(set(frequency_values)) == 1:
total_good_count = (total_good_count + 1) % modulo_value
# Return the final accumulated count modulo 10^9 + 7 as requested
return total_good_countTo count the good subsequences efficiently, we group identical items together and use combinatorics to calculate how many valid groups can be formed from each frequency. Instead of checking every possible combination one by one, we calculate the totals mathematically based on how often each item appears in the given collection.
Here's how the algorithm would work step-by-step:
from collections import Counter
def count_good_subsequences(word):
modulo_value = 10**9 + 7
character_counts = Counter(word)
frequency_counts = Counter(character_counts.values())
maximum_frequency = max(character_counts.values())
maximum_length = len(word)
combinations_table = [[0] * (maximum_length + 1) for _ in range(maximum_length + 1)]
for row_index in range(maximum_length + 1):
combinations_table[row_index][0] = 1
for column_index in range(1, row_index + 1):
combinations_table[row_index][column_index] = (combinations_table[row_index - 1][column_index - 1] + combinations_table[row_index - 1][column_index]) % modulo_value
# Precomputing combinations avoids redundant calculations for faster math execution.
total_good_subsequences = 0
for subsequence_size in range(1, maximum_frequency + 1):
ways_for_current_size = 1
for item_frequency, group_count in frequency_counts.items():
if item_frequency >= subsequence_size:
choices_from_group = combinations_table[item_frequency][subsequence_size]
ways_for_current_size = (ways_for_current_size * (choices_from_group + 1)) % modulo_value
# Subtracting one removes the empty subsequence counted by default in the product.
adjusted_ways = (ways_for_current_size - 1 + modulo_value) % modulo_value
total_good_subsequences = (total_good_subsequences + adjusted_ways) % modulo_value
# Modulo arithmetic prevents integer overflow issues during large summations.
return total_good_subsequences| Case | How to Handle |
|---|---|
| String with all identical characters such as aaaaa | The frequency array will have one non-zero entry, and the logic should sum combinations for that single frequency. |
| String with all unique characters such as abcde | Each character has a frequency of 1, resulting in combinations of choosing k characters from distinct frequencies. |
| Maximum length constraints up to 10 to the 5th power | Precomputing factorials and modular inverses is required to calculate combinations efficiently in linear time. |
| Large resulting values exceeding standard integer limits | All intermediate and final arithmetic operations must be taken modulo 10 to the 9th plus 7. |
| String with alternating or mixed duplicate frequencies such as aabbcc | Iterating through all possible common frequencies from 1 to the maximum frequency correctly captures all valid good subsequences. |
| String containing length of 1 | The single character forms one valid good subsequence of length 1, which the frequency counting and combination logic naturally handles. |