#C13442. Count Character Frequency
Count Character Frequency
Count Character Frequency
You are given a string and your task is to count the frequency of each character. The twist is that the characters should be considered in the order of their first appearance in the string. For example, for the string "hello", the character 'h' appears first followed by 'e', then 'l' and finally 'o'.
Task:
- Read a single line of input containing the string.
- Calculate the frequency of each distinct character, preserving the order in which they first appear.
- Output the number of unique characters in the string on the first line, followed by each unique character and its frequency on subsequent lines.
Formally, if the input string is denoted by \( s \), and the function that computes the frequency is defined as \[ f(s) = \{ c \rightarrow \text{number of occurrences of } c \text{ in } s \}, \] then the output must list the keys (characters) in the order of their first appearance in \( s \).
inputFormat
A single line containing the input string. The string may consist of letters, digits, special characters, or even be empty.
outputFormat
The output consists of multiple lines. The first line is an integer ( n ) representing the number of distinct characters in the input string. This is followed by ( n ) lines, each containing a character and its frequency separated by a space, in the order of their first appearance.## sample
hello
4
h 1
e 1
l 2
o 1
</p>