#C7168. 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 find the top (k) most frequent words from the list. The words must be sorted in descending order of frequency, and if two words have the same frequency, they should be sorted in ascending lexicographical order.
For example, if the input list is "i love leetcode i love coding" with (k = 2), the output should be "i love".
inputFormat
The input is given via standard input (stdin). The first line contains two integers (n) and (k), where (n) denotes the number of words and (k) is the number of top frequent words to output. The second line contains (n) words separated by spaces.
outputFormat
Output the top (k) frequent words in one line, separated by a single space. If the number of unique words is less than (k), output all unique words sorted as described.## sample
6 2
i love leetcode i love coding
i love