-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 104 ms (78.26%) | Memory: 62.4 MB (22.34%) - LeetSync
- Loading branch information
1 parent
4b75d29
commit cacc30c
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |