Given an array of strings words
and an integer k
, return the k
most frequent strings. Return the answer sorted by frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
words
?words
?k
be larger than the number of unique strings in words
?words
.k
elements based on frequency. For words with the same frequency, use lexicographical order to prioritize.k
elements since it allows for efficient insertion and removal.The overall time complexity is approximately O(N + M log K + K log K).
from typing import List
from collections import Counter
import heapq
def topKFrequent(words: List[str], k: int) -> List[str]:
# Step 1: Count the frequency of each word
freq = Counter(words)
# Step 2: Use a heap to find the k most frequent words
heap = []
for word, count in freq.items():
# Python's heapq module creates a min-heap, we use negative count to get max-heap behavior
heapq.heappush(heap, (-count, word))
# Step 3: Extract the top k frequent words from the heap
result = []
for _ in range(k):
result.append(heapq.heappop(heap)[1])
return result
# Example usage:
words = ["i", "love", "leetcode", "i", "love", "coding"]
k = 2
print(topKFrequent(words, k)) # Output: ["i", "love"]
This code will provide the k
most frequent words in the correct order, while handling ties by lexicographical sorting.
k
will always be valid, i.e., not greater than the number of unique words in the list. If k
can potentially be larger, additional checks may be needed.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?