A wonderful string is a string where at most one letter appears an odd number of times. This means all letters can appear an even number of times, or exactly one letter can appear an odd number of times while all others appear an even number of times.
Examples of wonderful strings:
"ccjjc"- 'c' appears 3 times (odd), 'j' appears 2 times (even)"abab"- 'a' appears 2 times (even), 'b' appears 2 times (even)"a"- 'a' appears 1 time (odd)
Not wonderful: "ab" - both 'a' and 'b' appear 1 time (two letters with odd counts)
Given a string word consisting of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in the word. Each occurrence of a substring is counted separately, even if the substring appears multiple times.
Input & Output
Visualization
Time & Space Complexity
Single pass through string, constant time hash map operations
Hash map stores at most n+1 bitmasks, but only 2^10=1024 possible bitmasks
Constraints
- 1 โค word.length โค 105
- word consists of only the first ten lowercase English letters ('a' through 'j')
- A substring is a contiguous sequence of characters within the string