-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1915.cpp
More file actions
29 lines (26 loc) · 762 Bytes
/
1915.cpp
File metadata and controls
29 lines (26 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
long long wonderfulSubstrings(string word) {
long long res = 0;
unordered_map<int, int> mask2cnt;
int bit = 0;
for (auto& c : word) {
int bitLoc = c - 'a';
bit ^= (1 << bitLoc);
// current
int cnt = __builtin_popcount(bit);
if (cnt == 0 || cnt == 1) res++;
// compare with previous ones
// case 1: difference by 1
for (int i = 0; i < 10; ++i) {
int temp = bit ^ (1 << i);
res += mask2cnt[temp];
}
// case 2: difference by 0
res += mask2cnt[bit];
// hash
mask2cnt[bit]++;
}
return res;
}
};