#C12921. Top K Frequent Words
Top K Frequent Words
Top K Frequent Words
Given a string (s) that may contain letters, spaces, punctuation, and an integer (k), your task is to find the (k) most frequent words in the string. All non-alphabetical characters are ignored and words are compared in a case-insensitive manner (i.e. all words are converted to lower-case).
In case two words have the same frequency, the word that is lexicographically smaller (i.e. comes first in alphabetical order) should appear earlier in the output.
(\textbf{Input:}) A single string and an integer (k).
(\textbf{Output:}) The (k) most frequent words separated by a space on one line.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
1. The first line contains the string (s), which may include spaces and punctuation.
2. The second line contains an integer (k), indicating the number of frequent words to output.
outputFormat
Output to standard output (stdout) a single line containing the (k) most frequent words separated by spaces. If there are fewer than (k) unique words, output all the words sorted accordingly.## sample
The sun shines over the lake. The sun sets
2
the sun