Skip to main content
Medium Heap / Priority Queue Medium frequency

Top K Frequent Words

Open on LeetCode

Approach Summary

Count frequencies, then min-heap of size k sorted by (frequency desc, lexicographic asc). Invert at end.

How to Recognize This Pattern

  • "Top k words by frequency, ties broken alphabetically"
  • Min-heap preserves k best seen so far

Complexity Analysis

Time Complexity

O(n log k)

Space Complexity

O(n)

Tags

Hash Table String Trie Sorting Heap Bucket Sort

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee