-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenges-109.js
52 lines (37 loc) · 1.33 KB
/
challenges-109.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
// // 49. Group Anagrams
// Given an array of strings strs, group the anagrams together. You can return the answer in any order.
// An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
// Example 1:
// Input: strs = ["eat","tea","tan","ate","nat","bat"]
// Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
// Example 2:
// Input: strs = [""]
// Output: [[""]]
// Example 3:
// Input: strs = ["a"]
// Output: [["a"]]
// #solution
function groupAnagrams(strs) {
const map = new Map();
for (let str of strs) {
// Sort the string to form the key
const sortedStr = str.split("").sort().join("");
// If the key is not present in the map, add it with an empty array
if (!map.has(sortedStr)) {
map.set(sortedStr, []);
}
// Push the original string to the group
map.get(sortedStr).push(str);
}
// Convert the map values to an array of arrays
return Array.from(map.values());
}
// Example 1
let strs1 = ["eat", "tea", "tan", "ate", "nat", "bat"];
console.log(groupAnagrams(strs1)); // Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
// Example 2
let strs2 = [""];
console.log(groupAnagrams(strs2)); // Output: [[""]]
// Example 3
let strs3 = ["a"];
console.log(groupAnagrams(strs3)); // Output: [["a"]]