#C8875. Top K Frequent Words
Top K Frequent Words
Top K Frequent Words
You are given a list of words and an integer ( k ). Your task is to determine the ( k ) most frequent words from the list. The words should be ordered by their frequency from highest to lowest. In the case of a tie, the words with the smaller lexicographical order should come first.
For example, if the input list is ["i", "love", "leetcode", "i", "love", "coding"] and ( k = 2 ), the answer will be ["i", "love"].
inputFormat
The first line of input contains an integer ( n ) indicating the total number of words.
The second line contains ( n ) space-separated words.
The third line contains an integer ( k ), representing the number of most frequent words to output.
outputFormat
Output the ( k ) most frequent words, each on a separate line, in order of descending frequency. If two words have the same frequency, output the lexicographically smaller word first.## sample
6
i love leetcode i love coding
2
i
love
</p>