An easy python solution!

  • 1
    from heapq import *
    class Solution(object):
        def topKFrequent(self, words, k):
            :type words: List[str]
            :type k: int
            :rtype: List[str]
            dic = {}
            for i in words:
                dic[i] = dic.get(i, 0) - 1
            tu = [(i, n) for n, i in dic.items()]
            h = nsmallest(k, tu)
            return [i[1] for i in h]

Log in to reply

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