javascript, nlog(n)


  • 0
    Y
    var frequencySort = function(str) {
        strArr = str.split('');
        let dic = {};
        strArr.forEach((x) => {
            if(dic.hasOwnProperty(x)) dic[x] = dic[x]+1;
            else dic[x] = 1;
        });
        strArr.sort((a, b) =>{
            let countDif = dic[b]-dic[a];
            if(!countDif) return a.charCodeAt(0)-b.charCodeAt(0);
            else return countDif;
        });
        return strArr.join('');
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.