#C12914. Most Common Words
Most Common Words
Most Common Words
You are given a text and an integer n. Your task is to write a program that finds the n most common words in the text and prints them in descending order of their frequency. Words are considered case-insensitive and punctuation must be ignored. For example, if the text is "Hello world! Hello everyone. It's a beautiful world." and n is 2, the output should be "hello world".
Note: If multiple words have the same frequency, their order in the output should follow their first occurrence in the processed text.
The frequency f of a word is defined as the number of times it appears in the text. Mathematically, for each word w: \[ f(w)=\text{count of } w \text{ in the text} \]
inputFormat
The input is given from stdin and consists of two lines:
- The first line contains a string representing the text. The text may include spaces and punctuation. It can also be empty.
- The second line contains an integer n indicating how many of the most common words to output.
outputFormat
Print the n most common words in one line, separated by a space, to stdout. If the resulting list is empty, print nothing.
## sampleHello world! Hello everyone. It's a beautiful world.
2
hello world