JavaScript Solution


  • 1
    S
    /**
     * @param {string} s
     * @return {string}
     */
    var frequencySort = function(s) {
        var map = {};
        var result = '';
        var stringArray = s.split('');
        //put the character count into a map
        for(var i=0;i<stringArray.length;i++){
        	map[stringArray[i]] = map[stringArray[i]] + 1 || 1;
        }
        //sort the map first, then push into the result
        Object.keys(map).sort((a,b)=>map[b]-map[a]).forEach(function(v){
        	for(var j=0;j<map[v];j++){
        		result += v;
        	}
        });
        
        return result;
    
    };
    

  • 0
    L

    The value in map can also be sorted. I am confused before see your solution. Thanks.


Log in to reply
 

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