Loading...
Loading...
Loading...
Given the words of customer searches, print the K words that were searched most often. Order them by count, highest first; words with the same count go in alphabetical order. If there are fewer than K different words, print all of them.
<word> <count>.Example
Input
10 2 the cat the dog the cat bird the dog cat
Output
the 4 cat 3
the appears 4 times and cat 3 times; dog 2 and bird 1 are lower.
Try the problem first. Open one hint at a time.
Count the words with a HashMap, then copy the entries into a list: new ArrayList<>(count.entrySet()).
Sort the list with a comparator: higher count first, and a.getKey().compareTo(b.getKey()) when the counts are equal.
Print Math.min(k, list.size()) lines.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor