Solution: Number of Wonderful Substrings
Let’s solve the Number of Wonderful Substrings problem using the Hash Maps pattern.
We'll cover the following
Statement
A wonderful string is defined as a string in which at most one letter occurs an odd number of times.
For example:
The string “aabbc” is wonderful because only the letter
'c'
appears an odd number of times.The string “pqpq” is wonderful because all letters appear an even number of times.
The string “mn” is not wonderful because both
'm'
and'n'
appear an odd number of times.
You are given a string word
that consists of lowercase English letters from 'a'
to 'j'
. Your task is to return the total number of wonderful non-empty word
.
Note: If a substring appears multiple times, each occurrence should be counted separately.
Constraints:
word.length
word
consists of lowercase English letters from'a'
to'j'
.
Solution
The solution uses a hash map to count the number of wonderful substrings in a given string word
. A wonderful substring is defined as a string in which at most one letter appears an odd number of times. The approach leverages a bitmask to track the parity of character frequencies efficiently.
Let’s go through the steps of the solution:
We create a hash map called
freq
to store the frequency of each bitmask. Each key in this hash map represents a bitmask that corresponds to the characters in the substring, while the value represents how many times that bitmask has been encountered.We initialize
freq[0] = 1
, whererepresents an empty prefix, which serves as a base case. We create a variable
mask
to maintain the current bitmask representing the frequency of characters as we iterate through the string.We also initialize a variable
res
withto store the count of wonderful substrings. We iterate through each character in
word
. For each character:We calculate the corresponding bit position, which gives us a value between
and for letters 'a'
to'j'
( since'a'
corresponds toand 'j'
corresponds to). We flip the parity of the bit at the calculated position in the
mask
using the XOR operation.
After updating the
mask
, we count substrings with zero odd occurrences by checking if the currentmask
exists in the frequency mapfreq
.If it does:
We add the count of that bitmask to
res
, which indicates the number of wonderful substrings ending at the current character.We then increment the count of the current
mask
infreq
.
If it doesn’t exist, we initialize it with a value of
.
To account for substrings that can have exactly one character with an odd count, we iterate through each possible character that could be odd (from
to ). For each character: We check if toggling its corresponding bit in the
mask
(using XOR again) results in a bitmask that exists infreq
. If it does, we add the frequency of that altered bitmask tores
.
Finally, after processing all characters in the string, we return
res
, which contains the total number of wonderful substrings found inword
.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.