Skip to content

Commit 808247f

Browse files
committed
refactor: use map
1 parent d87dac3 commit 808247f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

group-anagrams/tolluset.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
* SC: O(n * m)
44
* */
55
function groupAnagramsV2(strs: string[]): string[][] {
6-
const map: { [key: string]: string[] } = {};
6+
const map = new Map<string, string[]>();
77

88
const strSort = (str: string) => str.split("").sort().join("");
99

1010
for (const str of strs) {
1111
const sortedStr = strSort(str);
1212

13-
if (map[sortedStr]) {
14-
map[sortedStr].push(str);
13+
if (map.has(sortedStr)) {
14+
map.get(sortedStr)!.push(str);
1515
} else {
16-
map[sortedStr] = [str];
16+
map.set(sortedStr, [str]);
1717
}
1818
}
1919

20-
return Object.values(map);
20+
return Array.from(map.values());
2121
}
2222

2323
const tc1V2 = groupAnagramsV2(["eat", "tea", "tan", "ate", "nat", "bat"]); // [["bat"],["nat","tan"],["ate","eat","tea"]]

0 commit comments

Comments
 (0)