mirror of
https://github.com/prdlk/leetcode.git
synced 2026-08-02 17:31:40 +00:00
refactor(Hash Table): optimize character frequency sorting implementation
This commit is contained in:
@@ -52,5 +52,11 @@
|
||||
* @return {string}
|
||||
*/
|
||||
var frequencySort = function (s) {
|
||||
const freq = {};
|
||||
for (let c of s) freq[c] = (freq[c] || 0) + 1;
|
||||
|
||||
return s
|
||||
.split("")
|
||||
.sort((a, b) => freq[b] - freq[a] || a.localeCompare(b))
|
||||
.join("");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user