Skip to content

Commit 2b9a89b

Browse files
authored
Update 1915-number-of-wonderful-substrings.js
1 parent c850909 commit 2b9a89b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

1915-number-of-wonderful-substrings.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
/**
2+
* @param {string} word
3+
* @return {number}
4+
*/
5+
const wonderfulSubstrings = function(word) {
6+
const n = word.length;
7+
let mask = 0
8+
const hash = {0: 1}, a = 'a'.charCodeAt(0)
9+
let res = 0
10+
11+
for(let i = 0; i < n; i++) {
12+
const idx = word.charCodeAt(i) - a
13+
mask ^= (1 << idx)
14+
15+
res += hash[mask] || 0
16+
for(let j = 0; j < 10; j++) {
17+
const newMask = mask ^ (1 << j)
18+
res += hash[newMask] || 0
19+
}
20+
21+
if(hash[mask] == null) hash[mask] = 0
22+
hash[mask]++
23+
}
24+
25+
return res
26+
};
27+
28+
// another
29+
130
/**
231
* @param {string} word
332
* @return {number}

0 commit comments

Comments
 (0)