forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of Wonderful Substrings.js
28 lines (28 loc) · 1.12 KB
/
Number of Wonderful Substrings.js
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
/**
* @param {string} word
* @return {number}
*/
var wonderfulSubstrings = function(word) {
let hashMap={},ans=0,binaryRepresentation=0,t,pos,number,oneBitToggled;
hashMap[0]=1;
for(let i=0;i<word.length;i++){
pos = word[i].charCodeAt(0)-"a".charCodeAt(0);//Let's use position 0 for a, 1 for b .... 9 for j
t = (1 << pos);
binaryRepresentation = (binaryRepresentation^t);//Toggle the bit at pos in the current mask(pattern)
//This loop will check same binaryRepresentation and all the other binaryRepresentation with difference of 1 bit
for(let i=0;i<10;i++){//Check all the numbers by changing 1 position
number = (1<<i);//Change 1 bit at a time
oneBitToggled = (binaryRepresentation^number);
if(hashMap[oneBitToggled]!==undefined){
ans += hashMap[oneBitToggled];
}
}
if(hashMap[binaryRepresentation]===undefined){
hashMap[binaryRepresentation]=1;
}else{
ans += hashMap[binaryRepresentation];
hashMap[binaryRepresentation]++;
}
}
return ans;
};