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'.
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, where
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'.
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, where