알고리즘 공부 2020. 8. 20. 22:22

https://leetcode.com/problems/sort-characters-by-frequency/

 

Sort Characters By Frequency - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @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;
};
posted by Sense.J
: