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 3ee27d7 commit 977921dCopy full SHA for 977921d
group-anagrams/yoonthecoder.js
@@ -0,0 +1,14 @@
1
+var groupAnagrams = function (strs) {
2
+ const map = new Map();
3
+
4
+ for (const str of strs) {
5
+ // split the string into each character (returns an array) => sort it => convert it to string
6
+ const sortedStr = str.split('').sort().join('');
7
+ // use the sorted Str as unique keys in the map
8
+ if (map.has(sortedStr)) {
9
+ map.get(sortedStr).push(str);
10
+ } else map.set(sortedStr, [str]);
11
+ }
12
+ // convert values into an array
13
+ return [...map.values()];
14
+};
0 commit comments