#451. Sort Characters By Frequency
/**
* @param {string} s
* @return {string}
*/
var frequencySort = function (s) {
const freq = new Map
let res = ''
for (let char of s) {
if (!freq.has(char)) freq.set(char, '')
freq.set(char, freq.get(char) + char)
}
// return Array.from(freq.values()).sort((a,b) => b.length - a.length).toString().replaceAll(',', '')
for (let char of Array.from(freq.values()).sort((a,b) => b.length - a.length)) {
res += char
}
return res
};
Firstly, we store all number whether repeating or not under their keys in a put, the we get all values of the map, sort them according to their length in descending order then we append it to a result empty string and return the result.
It's quite similar to yesterday's solution
ย