Number of Wonderful Substrings LeetCode solution in JavaScript

1
/**
2
* @param {string} word
3
* @return {number}
4
*/
5
var wonderfulSubstrings = function (word) {
6
let hashMap = {},
7
ans = 0,
8
binaryRepresentation = 0,
9
t,
10
pos,
11
number,
12
oneBitToggled;
13
hashMap[0] = 1;
14
for (let i = 0; i < word.length; i++) {
15
pos = word[i].charCodeAt(0) - "a".charCodeAt(0); //Let's use position 0 for a, 1 for b .... 9 for j
16
t = 1 << pos;
17
binaryRepresentation = binaryRepresentation ^ t; //Toggle the bit at pos in the current mask(pattern)
18
//This loop will check same binaryRepresentation and all the other binaryRepresentation with difference of 1 bit
19
for (let i = 0; i < 10; i++) {
20
//Check all the numbers by changing 1 position
21
number = 1 << i; //Change 1 bit at a time
22
oneBitToggled = binaryRepresentation ^ number;
23
if (hashMap[oneBitToggled] !== undefined) {
24
ans += hashMap[oneBitToggled];
25
}
26
}
27
if (hashMap[binaryRepresentation] === undefined) {
28
hashMap[binaryRepresentation] = 1;
29
} else {
30
ans += hashMap[binaryRepresentation];
31
hashMap[binaryRepresentation]++;
32
}
33
}
34
return ans;
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0