Python Solution Beat 99% Submissions


  • 0
    J
    from collections import defaultdict
    class Solution(object):
        def topKFrequent(self, words, k):
            """
            :type words: List[str]
            :type k: int
            :rtype: List[str]
            """
            map = defaultdict(int)
            for word in words:
                map[word] += 1
            sort_items = sorted(map.items(), key = lambda x: (-x[1], x[0]))[:k]
            return [item[0] for item in sort_items]
    

    Any comments?


Log in to reply
 

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