알고리즘 공부
2020. 8. 20. 22:22
https://leetcode.com/problems/sort-characters-by-frequency/
/**
* @param {string} s
* @return {string}
*/
var frequencySort = function(s) {
const hash = {};
for( var i=0; i<s.length; ++i )
hash[ s.charAt(i) ] = 0;
for( var i=0; i<s.length; ++i )
++hash[ s.charAt(i) ];
var res = s.split('').sort( (a,b)=>{
return hash[b]-hash[a] !=0 ? hash[b]-hash[a] : b.charCodeAt(0) - a.charCodeAt(0);
}).join('');
return res;
};