A wonderful string is a string where at most one letter appears an odd number of times.
"ccjjc" and "abab" are wonderful, but "ab" is not.Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: word = "aba" Output: 4 Explanation: The four wonderful substrings are underlined below: - "aba" -> "a" - "aba" -> "b" - "aba" -> "a" - "aba" -> "aba"
Example 2:
Input: word = "aabb" Output: 9 Explanation: The nine wonderful substrings are underlined below: - "aabb" -> "a" - "aabb" -> "aa" - "aabb" -> "aab" - "aabb" -> "aabb" - "aabb" -> "a" - "aabb" -> "abb" - "aabb" -> "b" - "aabb" -> "bb" - "aabb" -> "b"
Example 3:
Input: word = "he" Output: 2 Explanation: The two wonderful substrings are underlined below: - "he" -> "h" - "he" -> "e"
Constraints:
1 <= word.length <= 105word consists of lowercase English letters from 'a' to 'j'.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 find special sections within a larger string. The brute force method looks at every possible section of the string, one by one. For each section, it checks if it's a 'wonderful' substring according to the problem's rules.
Here's how the algorithm would work step-by-step:
def number_of_wonderful_substrings_brute_force(word):
number_of_wonderful_substrings = 0
string_length = len(word)
for starting_index in range(string_length):
for ending_index in range(starting_index, string_length):
substring = word[starting_index : ending_index + 1]
character_counts = {}
# Count character frequencies in current substring
for char in substring:
character_counts[char] = character_counts.get(char, 0) + 1
odd_count_characters = 0
for count in character_counts.values():
#Check to see if we have too many odd frequency characters
if count % 2 != 0:
odd_count_characters += 1
if odd_count_characters <= 1:
# Substring is wonderful, increment counter
number_of_wonderful_substrings += 1
return number_of_wonderful_substringsThe challenge asks us to count special substrings within a bigger string. The smart way to solve it involves cleverly tracking the counts of characters to quickly identify and count the "wonderful" substrings that meet our conditions, rather than checking every possible substring directly. This avoids unnecessary work and makes the process much faster.
Here's how the algorithm would work step-by-step:
def number_of_wonderful_substrings(word):
number_of_characters = 10
state_counts = {0: 1}
current_state = 0
wonderful_substring_count = 0
for character in word:
character_index = ord(character) - ord('a')
current_state ^= (1 << character_index)
wonderful_substring_count += state_counts.get(current_state, 0)
# Check for substrings with one odd count.
for index in range(number_of_characters):
mask = 1 << index
flipped_state = current_state ^ mask
wonderful_substring_count += state_counts.get(flipped_state, 0)
# Update the count of current state.
state_counts[current_state] = state_counts.get(current_state, 0) + 1
return wonderful_substring_count| Case | How to Handle |
|---|---|
| Empty string input | Return 0, as an empty string has no substrings. |
| String with length of 1 | Check if the single character represents a 'wonderful' substring (i.e., has at most one odd count), and return 1 if so, 0 otherwise. |
| String with all identical characters | The number of wonderful substrings will depend on the character. If the character is the one represented by the bitmask, the count increases; otherwise, it's 0 or 1. |
| String with maximum allowed length | Ensure the solution uses a data structure (like a hash map) with efficient lookup and insertion, to prevent time limit exceedance. |
| String contains characters other than the first 10 lowercase letters | Ensure the bitmask calculation correctly handles such characters, perhaps by ignoring them or raising an exception. |
| Integer overflow when calculating the count of substrings | Use a 64-bit integer type (long in Java/C++) to store the count to prevent integer overflow. |
| The prefix frequency count is very high for some masks | Make sure the data structures used to store prefix frequencies can handle a large number of collisions or a high load factor. |
| No wonderful substrings exist | The algorithm should correctly return 0 in cases where no substring satisfies the 'wonderful' condition. |