We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1ce733b commit e1ae708Copy full SHA for e1ae708
group-anagrams/jun0811.js
@@ -0,0 +1,21 @@
1
+/**
2
+ * @param {string[]} strs
3
+ * @return {string[][]}
4
+ */
5
+var groupAnagrams = function (strs) {
6
+ const hashMap = new Map();
7
+ const res = [];
8
+ strs.forEach((str, index) => {
9
+ const sortedStr = [...str].sort().join('');
10
+ if (hashMap.has(sortedStr)) {
11
+ hashMap.set(sortedStr, [...hashMap.get(sortedStr), index]);
12
+ } else {
13
+ hashMap.set(sortedStr, [index]);
14
+ }
15
+ });
16
+ for (const [key, values] of hashMap) {
17
+ const anagrams = values.map((v) => strs[v]);
18
+ res.push(anagrams);
19
20
+ return res;
21
+};
0 commit comments