Skip to content

Commit 3747e58

Browse files
committed
Solve LeetCode 75 hash map / set problems
1 parent 1c07c4f commit 3747e58

File tree

6 files changed

+98
-13
lines changed

6 files changed

+98
-13
lines changed

LeetCode-75/.vscode/launch.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
// {
8-
// "type": "node",
9-
// "request": "launch",
10-
// "name": "Launch Program",
11-
// "skipFiles": [
12-
// "<node_internals>/**"
13-
// ],
14-
// "program": "${workspaceFolder}\\LeetCode-75\\19-724.js"
15-
// }
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": [
12+
"<node_internals>/**"
13+
],
14+
"program": "${workspaceFolder}\\23-2352.js"
15+
}
1616
]
1717
}

LeetCode-75/20-2215.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// https://leetcode.com/problems/find-the-difference-of-two-arrays/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const findDifference = (nums1: number[], nums2: number[]): number[][] => {
4+
const nums1Set = new Set(nums1),
5+
nums2Set = new Set(nums2);
6+
7+
return [
8+
[...nums1Set].filter((val) => !nums2Set.has(val)),
9+
[...nums2Set].filter((val) => !nums1Set.has(val)),
10+
];
11+
};

LeetCode-75/21-1207.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://leetcode.com/problems/unique-number-of-occurrences/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const uniqueOccurrences = (arr) => {
4+
const occurenceCountPerNb = new Map();
5+
6+
for (const val of arr) {
7+
if (!occurenceCountPerNb.has(val)) occurenceCountPerNb.set(val, 1);
8+
else occurenceCountPerNb.set(val, occurenceCountPerNb.get(val) + 1);
9+
}
10+
11+
const occurenceCountArr = Array.from(occurenceCountPerNb.values());
12+
13+
return new Set(occurenceCountArr).size === occurenceCountArr.length;
14+
};

LeetCode-75/22-1657.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// https://leetcode.com/problems/determine-if-two-strings-are-close/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const closeStrings = (word1: string, word2: string): boolean => {
4+
if (word1.length !== word2.length) return false;
5+
6+
const word1LettersFrequency = new Map(),
7+
word2LettersFrequency = new Map();
8+
9+
// O(N) SOLUTION
10+
for (let i = 0; i < word1.length; i++) {
11+
word1LettersFrequency.set(
12+
word1[i],
13+
word1LettersFrequency.has(word1[i])
14+
? word1LettersFrequency.get(word1[i]) + 1
15+
: 1
16+
);
17+
18+
word2LettersFrequency.set(
19+
word2[i],
20+
word2LettersFrequency.has(word2[i])
21+
? word2LettersFrequency.get(word2[i]) + 1
22+
: 1
23+
);
24+
}
25+
26+
const word1Arr = [
27+
...word1LettersFrequency.keys(),
28+
...word1LettersFrequency.values(),
29+
].sort(), // NOSONAR
30+
word2Arr = [
31+
...word2LettersFrequency.keys(),
32+
...word2LettersFrequency.values(),
33+
].sort(); // NOSONAR
34+
35+
return word1Arr.join("") === word2Arr.join("");
36+
};

LeetCode-75/23-2352.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/equal-row-and-column-pairs/?envType=study-plan-v2&envId=leetcode-75
2+
3+
const equalPairs = (grid: number[][]): number => {
4+
if (!grid.length || !grid[0].length) return 0;
5+
6+
const colsMap = new Map();
7+
8+
for (let i = 0; i < grid.length; i++) {
9+
let colStr = "";
10+
for (const row of grid) colStr += row[i] + ",";
11+
colStr = colStr.substring(0, colStr.length - 1);
12+
13+
colsMap.set(colStr, colsMap.has(colStr) ? colsMap.get(colStr) + 1 : 1);
14+
}
15+
16+
let pairsCount = 0;
17+
18+
for (let row of grid) {
19+
const rowStr = row.join(",");
20+
if (colsMap.has(rowStr)) pairsCount += colsMap.get(rowStr);
21+
}
22+
23+
return pairsCount;
24+
};

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535

3636
### Hash Map / Set
3737

38-
- [ ] 20. Find the Difference of Two Arrays
39-
- [ ] 21. Unique Number of Occurrences
40-
- [ ] 22. Determine if Two Strings Are Close
41-
- [ ] 23. Equal Row and Column Pairs
38+
- [X] 20. Find the Difference of Two Arrays
39+
- [X] 21. Unique Number of Occurrences
40+
- [X] 22. Determine if Two Strings Are Close
41+
- [X] 23. Equal Row and Column Pairs
4242

4343
### Stack
4444

0 commit comments

Comments
 (0)