-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupAnagrams.js
47 lines (36 loc) · 1.24 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
42
43
44
45
46
47
// Given an array of strings, group anagrams together.
// Example:
// Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
// Output:
// [
// ["ate","eat","tea"],
// ["nat","tan"],
// ["bat"]
// ]
// Note:
// All inputs will be in lowercase.
// The order of your output does not matter.
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
// for each item in array store it in an object - the key is the alphabetized version - value is an array
// then iterate through object to only return keys
let mappings = {};
for (let string of strs) {
let sortedString = string.split("").sort().join("");
if (mappings[sortedString]) {
mappings[sortedString].push(string);
} else {
mappings[sortedString] = [string];
}
}
let anagrams = [];
for (let [key, value] of Object.entries(mappings)) {
anagrams.push(value);
}
return anagrams;
};
// Time Complexity: O(NK \log K)O(NKlogK), where NN is the length of strs, and KK is the maximum length of a string in strs. The outer loop has complexity O(N)O(N) as we iterate through each string. Then, we sort each string in O(K \log K)O(KlogK) time.
// Space Complexity: O(NK)O(NK), the total information content stored in ans.