-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsherlock-and-anagrams.js
92 lines (79 loc) · 2.07 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
87
88
89
90
91
/*
*
* ------------------------
* | SHERLOCK AND ANAGRAMS |
* ------------------------
*
*
*
* Two strings are anagrams of each other if the
* letters of one string can be rearranged to form
* the other string. given a string, find the number of pairs
* of substrings of the string that are anagrams of each other.
* It must return an integer that represents the number
* of anagrammatic pairs of substrings
*
*
*
* e.g.
*
* function sherlockAndAnagrams(s) { // s = "abba"
* // should return 4 --> ["a","a"], ["b","b"], ["ab", "ba"], ["abb", "bba"]
* }
*
*
* */
/*
*
* ***** SOLUTION
*
* */
function sherlockAndAnagrams(s) {
// initialize anagramMap
// initialize range
// initialize anagramCounter
const anagramMap = {};
let range = 1;
let anagramCounter = 0;
// while loop until range is === s.length
while(range < s.length){
// populate map using for loop
for(let i = 0; i < s.length; i++){
if(s[(i + range) - 1]){
const key = s.slice(i, i + range).split("").sort().join("");
//console.log("range", `${i} - ${i + range} : ${key}`);
anagramMap[key] = anagramMap[key] + 1 || 1;
};
};
range ++;
};
// loop over anagram and find all keys that have values of greater than 1
// increase counter if they are
for(let key in anagramMap) {
const value = anagramMap[key];
if(value === 2) {
anagramCounter++;
}
if(value > 2) {
for(let i = 0; i < value; i++) {
for(let j = i + 1; j < value; j++) {
anagramCounter ++;
}
};
}
};
return anagramCounter;
}
/*
*
* ***** TESTS
*
* */
const s1 = "kkkk"; // 10
const s2 = "abba"; // 4
const s3 = "yuyuyyu";
const s4 = "osjfdlkksjf";
console.log("s1 --- ", sherlockAndAnagrams(s1));
console.log("s2 --- ", sherlockAndAnagrams(s2));
//console.log("s3 --- ", sherlockAndAnagrams(s3));
//console.log("s4 --- ", sherlockAndAnagrams(s4));