Top K Frequent Words - Problem
Top K Frequent Words
Given an array of strings
The result should be returned as a list where:
โข Words are sorted by frequency from highest to lowest
โข Words with the same frequency are sorted lexicographically (alphabetically)
For example, if we have
This is a classic problem that combines frequency counting, sorting, and priority queues - essential skills for coding interviews!
Given an array of strings
words and an integer k, you need to find the k most frequent strings from the array.The result should be returned as a list where:
โข Words are sorted by frequency from highest to lowest
โข Words with the same frequency are sorted lexicographically (alphabetically)
For example, if we have
["apple", "banana", "apple", "cherry", "banana", "apple"] and k=2, we should return ["apple", "banana"] because "apple" appears 3 times and "banana" appears 2 times.This is a classic problem that combines frequency counting, sorting, and priority queues - essential skills for coding interviews!
Input & Output
example_1.py โ Basic Case
$
Input:
words = ["i", "love", "leetcode", "i", "love", "coding"], k = 2
โบ
Output:
["i", "love"]
๐ก Note:
"i" and "love" are the two most frequent words. Both appear 2 times each. Since they have the same frequency, we need to break ties lexicographically: "i" comes before "love" alphabetically.
example_2.py โ Lexicographical Tie-breaking
$
Input:
words = ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
โบ
Output:
["the", "is", "sunny", "day"]
๐ก Note:
"the": 4 times, "is": 3 times, "sunny": 2 times, "day": 1 time. All have different frequencies so lexicographical ordering only matters within same frequency groups.
example_3.py โ All Same Frequency
$
Input:
words = ["apple", "banana", "cherry"], k = 2
โบ
Output:
["apple", "banana"]
๐ก Note:
All words appear exactly once, so we sort them lexicographically: "apple" < "banana" < "cherry", and take the first k=2.
Visualization
Tap to expand
Understanding the Visualization
1
Data Collection
Scan all posts and count hashtag frequencies, like a social media crawler
2
Smart Filtering
Use a priority system (heap) to keep track of only the top K trends, not all hashtags
3
Tie Breaking
When hashtags have the same popularity, display them alphabetically for consistency
4
Trending Page
Present the final top K trending hashtags in the correct order
Key Takeaway
๐ฏ Key Insight: Use a min-heap of size K to efficiently track top elements without sorting everything - just like how trending algorithms work in real social media platforms!
Time & Space Complexity
Time Complexity
O(n)
O(n) for counting + O(n) for bucketing + O(n) for collection
โ Linear Growth
Space Complexity
O(n)
O(n) for frequency map + O(n) for buckets
โก Linearithmic Space
Constraints
- 1 โค words.length โค 500
- 1 โค k โค number of unique words
- words[i] consists of lowercase English letters only
- 1 โค words[i].length โค 10
- Follow-up: Could you solve it in O(n log k) time and O(n) extra space?
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code