-
Notifications
You must be signed in to change notification settings - Fork 0
/
sherlock-and-anagrams.js
86 lines (67 loc) · 2.05 KB
/
sherlock-and-anagrams.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Complete the sherlockAndAnagrams function below.
function sherlockAndAnagrams(s) {
const anagramHash = {};
const strArr = Array.from(s);
let len = strArr.length;
const originalLen = len;
let i = 0;
let currStr = '';
let anagramsCount = 0;
let removedCharList = [];
let removedChar;
// find whether the given pattern is anagram and increment the count of it's relevant key
function addPatternToHash(pattern) {
let sortedStr = pattern.length > 1 ? [...pattern].sort((a, b) => a.localeCompare(b)).join('') : pattern;
if (!anagramHash[sortedStr]) {
anagramHash[sortedStr] = 1;
} else {
anagramHash[sortedStr]++;
}
}
// calculate anagram count.
function calculateAnagramsCount() {
const keys = Object.keys(anagramHash);
// filter the keys which are anagrams ( if the sorted key has more than or equal to 2 entries)
const pairKeys = keys.filter(key => anagramHash[key] >= 2);
pairKeys.forEach(key => {
let count = anagramHash[key];
if (key.length === 1) {
const p = findPair(count);
anagramsCount += p;
} else {
if (count === 2) {
anagramsCount += 1;
} else if (count > 2) {
anagramsCount += findPair(count);
}
}
});
}
while (strArr.length > 1) {
removedChar = strArr.splice(0, 1)[0];
removedCharList.push(removedChar);
const removedCharlen = removedCharList.length;
if (removedCharlen === originalLen) {
addPatternToHash(removedChar);
break;
}
addPatternToHash(strArr.join(''));
if (removedCharlen > 1) {
let tmp = [...removedCharList];
while (tmp.length > 1 ) {
addPatternToHash(tmp.join(''));
tmp.splice(0, 1);
}
}
addPatternToHash(removedChar);
len = strArr.length;
i = 0;
}
calculateAnagramsCount();
return anagramsCount;
}
// find the anagram pairs based on the presence of the sorted string.
function findPair(n) {
if (n === 1) return 0;
return n - 1 + findPair(n - 1);
}