-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathgroupAnagrams.js
41 lines (37 loc) · 1.38 KB
/
groupAnagrams.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
function groupAnagramsWithObject(arr) {
let anagramGroups = {};
for(let str of arr) {
let sortedString = str.split("").sort().join("");
let group = anagramGroups[sortedString];
if(group && group.length > 0) {
anagramGroups[sortedString].push(str);
} else {
anagramGroups[sortedString] = [str];
}
}
return Object.values(anagramGroups);
}
function groupAnagramsWithMap(arr) {
let anagramGroups = new Map();
for(let str of arr) {
let sortedString = str.split("").sort().join("");
if(anagramGroups.has(sortedString)) {
anagramGroups.get(sortedString).push(str);
} else {
anagramGroups.set(sortedString, [str]);
}
}
return Array.from(anagramGroups.values());
}
// Multiple Anagrams
console.log("Input: ['eat', 'tea', 'tan', 'ate', 'nat', 'bat']");
console.log("Output: ", groupAnagramsWithObject(['eat', 'tea', 'tan', 'ate', 'nat', 'bat']));
console.log("---------------");
// Mixed Case Anagrams
console.log("Input: ['Eat', 'Tea', 'Tan', 'Ate', 'Nat', 'Bat']");
console.log("Output: ", groupAnagramsWithObject(['Eat', 'Tea', 'Tan', 'Ate', 'Nat', 'Bat']));
console.log("---------------");
// No Anagrams
console.log("Input: ['apple', 'orange', 'banana']");
console.log("Output: ", groupAnagramsWithMap(['apple', 'orange', 'banana']));
console.log("---------------");