refactor(Hash Table): optimize character frequency sorting implementation

This commit is contained in:
Prad Nukala
2026-07-12 18:50:59 -04:00
parent b9e9bcff49
commit 4be2caee9f
@@ -45,12 +45,18 @@
* • 1 <= s.length <= 5 * 10^5 * • 1 <= s.length <= 5 * 10^5
* *
* • s consists of uppercase and lowercase English letters and digits. * • s consists of uppercase and lowercase English letters and digits.
*/ */
/** /**
* @param {string} s * @param {string} s
* @return {string} * @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("");
}; };