1915. Number of Wonderful Substrings
Asked at Uber
Difficulty
Medium
Time
O(n * 10)
Space
O(2^10)
Pattern
Hash Table
Companies
1
Problem
You are given a string word made only of the first ten lowercase letters, 'a' through 'j'. A string is called wonderful when at most one of its letters appears an odd number of times. All other letters must appear an even number of times (zero counts as even).
Return the number of non-empty substrings of word that are wonderful. A substring is a block of characters that sit next to each other in word. If the same block of characters appears in more than one place, count each place separately.
For example, in "aba" the wonderful substrings are the two single "a" blocks, the single "b", and the whole string "aba". That is 4.
Which substrings are wonderful?
Constraints
- ●
1 <= word.length <= 10^5 - ●
word consists of lowercase English letters from 'a' to 'j'
Examples
word = "aba"4word = "aabb"9word = "he"2Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Uber | Medium | View all Uber questions → |
How to Think About It
Brute force: check every substring, count odd-frequency letters — O(n^2 * 26)
Represent letter parity as a bitmask where bit i = 1 means letter i has odd count
Use prefix XOR: prefix[j] XOR prefix[i] gives parity of substring [i, j)
A substring is wonderful if the XOR result has at most one bit set
Count occurrences of each prefix mask, use hashmap, for each mask check mask, mask^(1<<k)
Optimal Approach
Use a bitmask to track parity of each letter. Build prefix XOR where each bit represents odd/even count of that letter. A substring [i,j] is wonderful if prefix[j] XOR prefix[i] has at most one set bit. Use a hashmap to count prefix mask occurrences. For each mask, add counts of the same mask and all masks that differ by exactly one bit. This gives O(n * 26) time.
Prefix parity masks
What Trips People Up in Real Interviews
Clarify: single character substrings are always wonderful
Bit manipulation insight: number with at most one set bit = 0 or power of 2
prefix XOR stores parity of counts up to current position
Initialize count map with {0: 1} for empty prefix
For each position, add counts of masks that differ by at most one bit
Solution Code
O(n * 10) timeclass Solution:
def wonderfulSubstrings(self, word: str) -> int:
from collections import Counter
count = Counter()
count[0] = 1
mask = 0
result = 0
for ch in word:
mask ^= 1 << (ord(ch) - ord('a'))
result += count[mask]
for k in range(10):
result += count[mask ^ (1 << k)]
count[mask] += 1
return resultFrequently Asked Questions
What is the Number of Wonderful Substrings problem?
A wonderful string is one where at most one letter appears an odd number of times. Given a string word consisting of lowercase English letters, return the number of wonderful non-empty substrings. A substring is a contiguous sequence of characters within the string.
How do you solve Number of Wonderful Substrings?
The optimal approach runs in O(n * 10) time and O(2^10) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Number of Wonderful Substrings?
Number of Wonderful Substrings is asked at Uber. It is a medium difficulty problem.
What are common mistakes on Number of Wonderful Substrings?
- Clarify: single character substrings are always wonderful
- Bit manipulation insight: number with at most one set bit = 0 or power of 2
- prefix XOR stores parity of counts up to current position
- Initialize count map with {0: 1} for empty prefix
- For each position, add counts of masks that differ by at most one bit