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:
@@ -45,12 +45,18 @@
|
||||
* • 1 <= s.length <= 5 * 10^5
|
||||
*
|
||||
* • s consists of uppercase and lowercase English letters and digits.
|
||||
*/
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {string}
|
||||
*/
|
||||
var frequencySort = function(s) {
|
||||
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