Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
A wonderful string is a string where at most one letter appears an odd number of times.
"ccjjc" and "abab" are wonderful, but "ab" is not.Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: word = "aba" Output: 4 Explanation: The four wonderful substrings are underlined below: - "aba" -> "a" - "aba" -> "b" - "aba" -> "a" - "aba" -> "aba"
Example 2:
Input: word = "aabb" Output: 9 Explanation: The nine wonderful substrings are underlined below: - "aabb" -> "a" - "aabb" -> "aa" - "aabb" -> "aab" - "aabb" -> "aabb" - "aabb" -> "a" - "aabb" -> "abb" - "aabb" -> "b" - "aabb" -> "bb" - "aabb" -> "b"
Example 3:
Input: word = "he" Output: 2 Explanation: The two wonderful substrings are underlined below: - "he" -> "h" - "he" -> "e"
Constraints:
1 <= word.length <= 105word consists of lowercase English letters from 'a' to 'j'.Problem summary: A wonderful string is a string where at most one letter appears an odd number of times. For example, "ccjjc" and "abab" are wonderful, but "ab" is not. Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately. A substring is a contiguous sequence of characters in a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation
"aba"
"aabb"
"he"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1915: Number of Wonderful Substrings
class Solution {
public long wonderfulSubstrings(String word) {
int[] cnt = new int[1 << 10];
cnt[0] = 1;
long ans = 0;
int st = 0;
for (char c : word.toCharArray()) {
st ^= 1 << (c - 'a');
ans += cnt[st];
for (int i = 0; i < 10; ++i) {
ans += cnt[st ^ (1 << i)];
}
++cnt[st];
}
return ans;
}
}
// Accepted solution for LeetCode #1915: Number of Wonderful Substrings
func wonderfulSubstrings(word string) (ans int64) {
cnt := [1024]int{1}
st := 0
for _, c := range word {
st ^= 1 << (c - 'a')
ans += int64(cnt[st])
for i := 0; i < 10; i++ {
ans += int64(cnt[st^(1<<i)])
}
cnt[st]++
}
return
}
# Accepted solution for LeetCode #1915: Number of Wonderful Substrings
class Solution:
def wonderfulSubstrings(self, word: str) -> int:
cnt = Counter({0: 1})
ans = st = 0
for c in word:
st ^= 1 << (ord(c) - ord("a"))
ans += cnt[st]
for i in range(10):
ans += cnt[st ^ (1 << i)]
cnt[st] += 1
return ans
// Accepted solution for LeetCode #1915: Number of Wonderful Substrings
/**
* [1915] Number of Wonderful Substrings
*
* A wonderful string is a string where at most one letter appears an odd number of times.
*
*
* For example, "ccjjc" and "abab" are wonderful, but "ab" is not.
*
*
* Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.
*
* A substring is a contiguous sequence of characters in a string.
*
*
* Example 1:
*
*
* Input: word = "aba"
* Output: 4
* Explanation: The four wonderful substrings are underlined below:
* - "<u>a</u>ba" -> "a"
* - "a<u>b</u>a" -> "b"
* - "ab<u>a</u>" -> "a"
* - "<u>aba</u>" -> "aba"
*
*
* Example 2:
*
*
* Input: word = "aabb"
* Output: 9
* Explanation: The nine wonderful substrings are underlined below:
* - "<u>a</u>abb" -> "a"
* - "<u>aa</u>bb" -> "aa"
* - "<u>aab</u>b" -> "aab"
* - "<u>aabb</u>" -> "aabb"
* - "a<u>a</u>bb" -> "a"
* - "a<u>abb</u>" -> "abb"
* - "aa<u>b</u>b" -> "b"
* - "aa<u>bb</u>" -> "bb"
* - "aab<u>b</u>" -> "b"
*
*
* Example 3:
*
*
* Input: word = "he"
* Output: 2
* Explanation: The two wonderful substrings are underlined below:
* - "<u>h</u>e" -> "h"
* - "h<u>e</u>" -> "e"
*
*
*
* Constraints:
*
*
* 1 <= word.length <= 10^5
* word consists of lowercase English letters from 'a' to 'j'.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-wonderful-substrings/
// discuss: https://leetcode.com/problems/number-of-wonderful-substrings/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn wonderful_substrings(word: String) -> i64 {
word.bytes()
.fold(
(0usize, 0i64, {
let mut v = vec![0i64; 1024];
v[0] = 1;
v
}),
|(mut x, mut ans, mut v), c| {
x ^= 1 << (c - 97);
ans += v[x];
ans += (0..10).map(|i| v[x ^ (1 << i)]).sum::<i64>();
v[x] += 1;
(x, ans, v)
},
)
.1
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1915_example_1() {
let word = "aba".to_string();
let result = 4;
assert_eq!(Solution::wonderful_substrings(word), result);
}
#[test]
fn test_1915_example_2() {
let word = "aabb".to_string();
let result = 9;
assert_eq!(Solution::wonderful_substrings(word), result);
}
#[test]
fn test_1915_example_3() {
let word = "he".to_string();
let result = 2;
assert_eq!(Solution::wonderful_substrings(word), result);
}
}
// Accepted solution for LeetCode #1915: Number of Wonderful Substrings
function wonderfulSubstrings(word: string): number {
const cnt: number[] = new Array(1 << 10).fill(0);
cnt[0] = 1;
let ans = 0;
let st = 0;
for (const c of word) {
st ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
ans += cnt[st];
for (let i = 0; i < 10; ++i) {
ans += cnt[st ^ (1 << i)];
}
cnt[st]++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.