Skip to main content

692. Top K Frequent Words

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

Python

  • Time: O(N)
  • Space: O(N)
from collections import Counter, defaultdict


class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
counts = [(-item[1], item[0]) for item in Counter(words).items()]
counts.sort()

return [item[1] for item in counts[:k]]