#C13521. Find Most Common Words
Find Most Common Words
Find Most Common Words
You are given a text string and an integer n. Your task is to identify the n most frequently occurring words in the text.
The rules are as follows:
- Words are considered case-insensitive (e.g. "Apple" and "apple" are the same).
- Punctuation should be ignored.
- If several words have the same frequency, they should be sorted in alphabetical order (using lexicographical order).
- If the text is empty or n is greater than the number of unique words, simply output all the words available.
Note: The output should list the words in one line separated by a single space.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains the text string. It may include punctuation and mixed case letters.
- The second line contains an integer n, the number of top frequent words to output.
outputFormat
Print the n most frequent words to standard output (stdout) in one line, separated by a single space. If the text is empty, print an empty line.
## sampleapple banana apple orange apple banana
2
apple banana