#C13017. Word Frequency Counter
Word Frequency Counter
Word Frequency Counter
You are given n strings. Your task is to extract all words from these strings, where a word is defined as a contiguous sequence of alphabetical characters (A-Z and a-z). All words should be converted to lowercase.
Count the frequency of each distinct word and then output each word with its frequency, one per line. The output must be sorted in descending order of frequency. If two words have the same frequency, they should be sorted in ascending lexicographical order (i.e., dictionary order).
For clarity, if a word appears f times, it is represented in the output as:
\( word \; f \)
Note: Use the regular expression \b[a-zA-Z]+\b
to extract words and handle case insensitivity by converting letters to lowercase.
inputFormat
The input is read from stdin
and has the following format:
n s1 s2 ... sn
Here, n
(an integer) is the number of strings, and each si
is a string which may contain letters, numbers, punctuation, or other characters.
outputFormat
Print the results to stdout
as follows. For each unique word extracted from the input, print a line that contains the word (in lowercase) followed by a space and the frequency count. The words should be printed in order of descending frequency, and if frequencies are equal, in ascending lexicographical order.
6
Hello
world
hello!
WORLD
world123
hello 2
world 2
</p>