Problem Description
Given a string made up of the first ten lowercase English letters ('a' to 'j'), count the number of non-empty substrings that are "wonderful". A substring is considered wonderful if at most one character in it appears an odd number of times.
Key Insights
- Use a bitmask to represent the parity (even/odd occurrence) for each of the ten letters.
- A prefix-based approach allows us to convert the problem of checking substrings into comparing prefix bitmasks.
- When the XOR of the bitmask for two prefixes yields 0 (or has only one bit set), it indicates that the corresponding substring is wonderful.
- Use a hash table (or dictionary) to store and count how many times a particular bitmask has appeared.
Space and Time Complexity
Time Complexity: O(n * 10) ≈ O(n), where n is the length of the input string. Space Complexity: O(2^10) = O(1) since there are at most 1024 distinct bitmask values.
Solution
We utilize a bitmask to monitor the frequency parity of each letter in the given string. As we iterate through the string, we update the bitmask by flipping the bit corresponding to the current letter. A hash table (or dictionary) is maintained to track the number of times each bitmask occurs as a prefix. For the current bitmask:
- Adding the count of the same bitmask seen before accounts for substrings where all characters have even frequency.
- Flipping each bit (one at a time) and adding the count from the hash table counts the substrings where exactly one character has an odd frequency. This efficient use of bit manipulation along with prefix sums allows us to check all substrings without explicitly enumerating them.