#K46942. Word Frequencies
Word Frequencies
Word Frequencies
You are given a document consisting of n lines of text. Your task is to compute the frequency of each word that appears in the document and display them in alphabetical order.
The words in the document are separated by whitespace. Note that the comparison is case sensitive, and punctuation is considered part of the word. The output should list each unique word along with its frequency, where each line of the output contains the word followed by a space and then its count.
You need to implement a solution that reads from stdin
and writes the result to stdout
.
Formally, if the document contains words \(w_1, w_2, \dots, w_k\) (extracted by splitting on whitespace), then for each unique word \(w\) let \(f(w)\) be its frequency. The output should list each word \(w\) in lexicographical order along with \(f(w)\).
inputFormat
The input is read from stdin
and has the following format:
n line_1 line_2 ... line_n
The first line contains an integer n which represents the number of lines that follow. Each of the next n lines contains a sequence of characters (words separated by whitespace).
outputFormat
The output is written to stdout
and consists of several lines. Each line contains a word and its frequency separated by a single space. The words must be listed in alphabetical order.
For example, if the word "Hello" appears 3 times, one of the output lines should be:
Hello 3## sample
3
Hello world
world of coding
Hello Hello
Hello 3
coding 1
of 1
world 2
</p>