#K41437. Word Frequency Counter
Word Frequency Counter
Word Frequency Counter
Given a string of text that may contain punctuation, numbers, and spaces, your task is to count the occurrences of each word in the string. Only alphabetical characters (a-z and A-Z) are considered part of a word. All words should be treated as case-insensitive (i.e. converted to lowercase).
Details:
- Non-alphabetical characters (such as punctuation and numbers) should be treated as delimiters.
- Words are sequences of alphabetical characters.
- The output should list each word and its count, with the words sorted in lexicographical (alphabetical) order.
Mathematical Formulation:
Let \( S \) be the input string. For each word \( w \) extracted from \( S \), let \( f(w) \) denote its frequency. The output is the set \( \{(w, f(w)) \mid w \in W \} \) where \( W \) is the set of distinct words from \( S \), sorted in ascending order.
inputFormat
The input is a string read from standard input (stdin
). The string may span multiple lines.
outputFormat
For each distinct word in the input, output a line containing the word in lowercase, a single space, and its frequency count. The words must appear in lexicographical (alphabetical) order. Output to standard output (stdout
).
Hello, hello! How are you? I hope you are well. Well, well, well...
are 2
hello 2
how 1
i 1
hope 1
well 4
you 2
</p>