Skip to content

Commit d9c6ac9

Browse files
committed
Solved group anagram / missing number
1 parent 5d3d672 commit d9c6ac9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

group-anagrams/nhistory.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var groupAnagrams = function (strs) {
2+
// Declare hash map to store sorted strs
3+
let map = new Map();
4+
5+
for (let str of strs) {
6+
// Sorted each str
7+
const sortedStr = str.split("").sort().join("");
8+
9+
// If there is alread sortedStr on the map, pushed str
10+
if (map.has(sortedStr)) {
11+
map.get(sortedStr).push(str);
12+
} else {
13+
// If there is no sortedStr on the map, insert [str]
14+
map.set(sortedStr, [str]);
15+
}
16+
}
17+
return Array.from(map.values());
18+
};
19+
20+
// TC: O(n*klogk)
21+
// SC: O(n*k)
22+
// n -> length of strs array
23+
// k -> amount of character for each element

missing-number/nhistory.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var missingNumber = function (nums) {
2+
// Get a expected summation
3+
const n = nums.length;
4+
const expectedSum = (n * (n + 1)) / 2;
5+
6+
// Calculate summation of nums
7+
let numsSum = 0;
8+
for (let i = 0; i < n; i++) {
9+
numsSum += nums[i];
10+
}
11+
12+
return expectedSum - numsSum;
13+
};
14+
15+
// TC: O(n)
16+
// SC: O(1)

0 commit comments

Comments
 (0)