Because we want to find the top k highest frequency, this is related to ordering. Thus we can use some sorted or partially sorted data structure.
We want to store the entries with comparing the frequency.
Thus, we can use a minHeap.
time: O(nlogn)
space: O(n)
We want to store top k elements uptill the current element.
We have to record the string's frequency, thus we can use hashmap to store the string and its frequency.
Also we need to update the current topk. We can use a treeset to store the topk elements.
time: record: O(logk) topK: O(k) space: O(n + k) n is the number of strings
time: record: O(1) topK: O(k) space: O(n + k)