Skip to content

Commit

Permalink
Time: 104 ms (78.26%) | Memory: 62.4 MB (22.34%) - LeetSync
Browse files Browse the repository at this point in the history
  • Loading branch information
ShatilKhan committed Feb 6, 2024
1 parent 4b75d29 commit cacc30c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 49-group-anagrams/group-anagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @param {string} s
* @return {string}
*/

var getSignature = function(s) {
const count = Array(26).fill(0);
for (const c of s){
count[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
}

const result = [];
for (let i=0; i < 26; i++) {
if (count[i] !== 0) {
result.push(String.fromCharCode(i + 'a'.charCodeAt(0)), count[i].toString());
}
}
return result.join('');
} ;



/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const result = [];
const groups = new Map();

for (const s of strs) {
const signature = getSignature(s);
if(!groups.has(signature)){
groups.set(signature, []);
}
groups.get(signature).push(s);
}

groups.forEach(value => result.push(value));

return result;
};

0 comments on commit cacc30c

Please sign in to comment.