A Count-Min Sketch estimates frequencies in sublinear space, but it cannot enumerate keys. Combine it with a bounded Misra-Gries candidate tracker to report likely heavy hitters from a stream.
Design CountMinHeavyHitterFinder.topK(width, depth, items, candidateCapacity, k). Use the polynomial hash h = (h * 131 + characterCode) & 0x7fffffff. Row r updates column hash(decimal(r) + ":" + item) % width.
Maintain at most candidateCapacity candidate counters. For each item: increment it when present; otherwise add it with count one if space remains; otherwise decrement every candidate and remove zeros. These counters control admission only. After the stream, estimate every survivor by the minimum sketch counter across rows. Sort by decreasing estimate, breaking ties lexicographically, and return the first k strings.
Example 1:
Example 2:
Constraints
1 <= width, depth <= 10^30 <= items.length <= 10^51 <= candidateCapacity <= 10^3, 1 <= k <= 10^3- Items contain printable ASCII and have length from
1 to 100.