Skip to content

Commit cfb842c

Browse files
committed
Add group-anagrams solution
1 parent 181a129 commit cfb842c

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

group-anagrams/Jeehay28.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Guided approach
2+
// TC : O(n*k), where n is the number of strings, and k is the average length of each string.
3+
// SC : O(n*k)
4+
// overal time complexity improved : from O(n * klogk) to O(n * k)
5+
6+
/**
7+
* Time Complexity Breakdown:
8+
*
9+
* Step | Time Complexity | Explanation
10+
* --------------------------------------- | ------------------- | ----------------------------------------
11+
* Outer loop over strings (`for` loop) | O(n) | Iterate over each string in the input array `strs`.
12+
* Create key (`createKey`) | O(k) per string | For each string, count character frequencies, with k being the length of the string.
13+
* Map operations (`set` and `get`) | O(1) per string | Inserting and retrieving values from a Map.
14+
* Result array | O(n * k) | Storing grouped anagrams in the result array.
15+
*
16+
* Overall Time Complexity: | O(n * k) | Total time complexity considering all steps.
17+
*
18+
* Space Complexity Breakdown:
19+
*
20+
* Step | Space Complexity | Explanation
21+
* --------------------------------------- | ------------------- | -----------------------------------------
22+
* Map to store grouped anagrams | O(n * k) | Map stores n groups with each group having at most k characters.
23+
* Auxiliary space for `createKey` | O(1) | The frequency array used to count characters (constant size of 26).
24+
* Space for the result array | O(n * k) | Result array storing n groups of up to k elements.
25+
*
26+
* Overall Space Complexity: | O(n * k) | Total space complexity considering all storage.
27+
*/
28+
29+
/**
30+
* @param {string[]} strs
31+
* @return {string[][]}
32+
*/
33+
34+
var groupAnagrams = function (strs) {
35+
const createKey = (str) => {
36+
const arr = new Array(26).fill(0);
37+
38+
for (const ch of str) {
39+
const idx = ch.charCodeAt() - "a".charCodeAt();
40+
arr[idx] += 1;
41+
}
42+
43+
return arr.join("#");
44+
};
45+
46+
let map = new Map();
47+
48+
for (const str of strs) {
49+
const key = createKey(str);
50+
map.set(key, [...(map.get(key) || []), str]);
51+
}
52+
53+
return Array.from(map.values(map));
54+
};
55+
56+
// *My own approach
57+
58+
// Time Complexity
59+
// 1. Sorting Each String:
60+
// Sorting a string takes O(k*logk), where k is the length of the string.
61+
// Since we sort each string in the input array of size n, the total cost for sorting is O(n*klogk).
62+
63+
// 2. Hash Map Operations:
64+
// Inserting into the hash map is O(1) on average. Over n strings, the cost remains O(n).
65+
66+
// Overall Time Complexity:
67+
// O(n*klogk), where n is the number of strings and k is the average length of a string.
68+
69+
// /**
70+
// * @param {string[]} strs
71+
// * @return {string[][]}
72+
// */
73+
74+
// var groupAnagrams = function (strs) {
75+
// // helper function
76+
// const sorted = (str) => {
77+
// return str.split("").sort().join("");
78+
// };
79+
80+
// let obj = {};
81+
82+
// for (const str of strs) {
83+
// const key = sorted(str);
84+
// obj[key] = [...(obj[key] || []), str];
85+
// }
86+
87+
// return Object.values(obj);
88+
// };

0 commit comments

Comments
 (0)