Number of Wonderful Substrings - Problem

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

example_1.py โ€” Basic Case
$ Input: word = "aba"
โ€บ Output: 4
๐Ÿ’ก Note: The wonderful substrings are: "a" (position 0), "b" (position 1), "a" (position 2), and "aba" (entire string). Note that "ab" and "ba" are not wonderful since both have two characters with odd counts.
example_2.py โ€” All Even Counts
$ Input: word = "aabb"
โ€บ Output: 9
๐Ÿ’ก Note: All possible substrings where at most one character appears an odd number of times: "a", "a", "b", "b" (single chars), "aa", "bb" (pairs), "aab", "abb" (three chars), "aabb" (full string).
example_3.py โ€” Single Character
$ Input: word = "he"
โ€บ Output: 2
๐Ÿ’ก Note: The wonderful substrings are "h" and "e". The substring "he" is not wonderful because both 'h' and 'e' appear an odd number of times (once each).

Visualization

Tap to expand
๐ŸŽฏ Light Switch Panel AnalogySwitch Panel for "aba"Each switch represents a letter (a-j). ON = odd count, OFF = even countWonderful = at most 1 switch ONProcessing "aba":Start0000000000โ†’Read 'a'0000000001โ†’Read 'b'0000000011โ†’Read 'a'0000000010Finding Wonderful Substrings:1. Position 0 ('a'): bitmask=1, check hash {0:1} โ€ข 1 vs 0: differ by 1 bit โœ“ โ†’ 1 wonderful substring2. Position 1 ('b'): bitmask=3, check hash {0:1, 1:1} โ€ข 3 vs 1: differ by 1 bit โœ“ โ†’ 1 more wonderful substring3. Position 2 ('a'): bitmask=2, check hash {0:1, 1:1, 3:1} โ€ข 2 vs 0: differ by 1 bit โœ“ โ€ข 2 vs 3: differ by 1 bit โœ“ โ†’ 2 more๐ŸŽฏ Key Insight:XOR naturally flips bits, and hash map efficiently finds compatible patterns!Total: 4 wonderful substrings
Understanding the Visualization
1
Initialize State
Start with all switches OFF (bitmask = 0)
2
Process Characters
For each character, flip its corresponding switch
3
Check Compatibility
Look for previous states that match or differ by one switch
4
Count Results
Add compatible pairs to the total count
Key Takeaway
๐ŸŽฏ Key Insight: By representing character parity as bits and using XOR operations, we can efficiently track state changes and find compatible substring pairs in O(n) time!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through string, constant time hash map operations

n
2n
โœ“ Linear Growth
Space Complexity
O(min(n, 2^10))

Hash map stores at most n+1 bitmasks, but only 2^10=1024 possible bitmasks

n
2n
โšก Linearithmic Space

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
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
42.8K Views
Medium-High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen