Updating exercises
This commit is contained in:
18
src/exercises/49.group-anagrams.js
Normal file
18
src/exercises/49.group-anagrams.js
Normal file
@@ -0,0 +1,18 @@
|
||||
// @leet start
|
||||
/**
|
||||
* @param {string[]} strs
|
||||
* @return {string[][]}
|
||||
*/
|
||||
var groupAnagrams = function(strs) {
|
||||
if (strs.length < 2) return [strs];
|
||||
const map = {};
|
||||
for (const str of strs) {
|
||||
const key = str.split("").sort().join("");
|
||||
if (!map[key]) {
|
||||
map[key] = [];
|
||||
}
|
||||
map[key].push(str);
|
||||
}
|
||||
return Object.values(map);
|
||||
};
|
||||
// @leet end
|
||||
Reference in New Issue
Block a user