Easy to understand Python code

  • 0

    An easy to understand Python code.
    However, I believe there must be many ways to make this code faster :).
    please post your suggestions.
    Thanks in advance

    import operator
    class Solution(object):
        def frequencySort(self, s):
           x = {}
           for k in s:
             x[k] = x.get(k, 0) + 1
           sorted_x = sorted(x.items(), key=operator.itemgetter(1), reverse=True)
           result = ""
           for i in sorted_x:
               result = result + i[0] * i[1]
           return result

Log in to reply

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