Leetcode 692 Solution

This article provides solution to leetcode question 692 (top-k-frequent-words)

https://leetcode.com/problems/top-k-frequent-words

Solution

class Solution:
    def topKFrequent(self, words: List[str], k: int) -> List[str]:
        m = collections.defaultdict(int)
        for word in words:
            m[word] += 1

        d = []
        for word, cnt in m.items():
            d.append((-cnt, word))
        d.sort()

        return [word for _, word in d[:k]]