class Solution:
def wonderfulSubstrings(self, word: str) -> int:
from collections import defaultdict
count = defaultdict(int)
count[0] = 1
res = 0
mask = 0
for ch in word:
mask ^= 1 << (ord(ch) - ord('a'))
res += count[mask]
for i in range(10):
res += count[mask ^ (1 << i)]
count[mask] += 1
return res
class Solution {
public:
long long wonderfulSubstrings(string word) {
vector<long long> count(1024, 0);
count[0] = 1;
int mask = 0;
long long res = 0;
for (char ch : word) {
mask ^= 1 << (ch - 'a');
res += count[mask];
for (int i = 0; i < 10; ++i) {
res += count[mask ^ (1 << i)];
}
count[mask]++;
}
return res;
}
};
class Solution {
public long wonderfulSubstrings(String word) {
long[] count = new long[1024];
count[0] = 1;
int mask = 0;
long res = 0;
for (char ch : word.toCharArray()) {
mask ^= 1 << (ch - 'a');
res += count[mask];
for (int i = 0; i < 10; i++) {
res += count[mask ^ (1 << i)];
}
count[mask]++;
}
return res;
}
}
var wonderfulSubstrings = function(word) {
const count = new Array(1024).fill(0);
count[0] = 1;
let res = 0, mask = 0;
for (let ch of word) {
mask ^= 1 << (ch.charCodeAt(0) - 'a'.charCodeAt(0));
res += count[mask];
for (let i = 0; i < 10; i++) {
res += count[mask ^ (1 << i)];
}
count[mask]++;
}
return res;
};
word
consisting only of lowercase English letters from 'a' to 'j', a substring is called wonderful if at most one letter appears an odd number of times in it. The task is to count the total number of wonderful substrings in word
.
word
.word = "aba"
:
mask = 0
and count[0] = 1
.mask = 1
.mask = 3
(binary 11).mask = 2
(binary 10).