Solution Python


  • 0
    J
    def frequencySort(self, s):
        result = []
        for letter, times in sorted(Counter(s).iteritems(), key = lambda x: x[1], reverse = True):
            result += [letter] * times
        return ''.join(result)
    

    The reason I chose this approach was because when I tried to just sort it regularly using Counter as a key it would mix up the letters that had the same frequency (so if the word was: "helloo" it would show somehting like loloeh) I don't know if that's intended or not, but this way you make sure the letters are being repeated together


Log in to reply
 

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