#C6970. Most Frequent Words
Most Frequent Words
Most Frequent Words
Given an integer k and a text string, your task is to identify the k most frequently occurring words in the text.
The text may contain punctuation and mixed case letters. You should ignore punctuation and treat words in a case-insensitive manner. In case of ties in the frequency, sort the words in alphabetical order.
Note: Words are defined as sequences of alphanumeric characters (matching the regex \w+
).
Example:
For k = 3
and text "hello world! hello code. code, hello.", the answer is: hello code world
.
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains an integer k, the number of most frequent words to output.
- The second line contains the text string from which words are to be extracted.
outputFormat
Print the k most frequent words on a single line, separated by a single space.
## sample3
hello world! hello code. code, hello.
hello code world