#C5382. Character Frequency Counter
Character Frequency Counter
Character Frequency Counter
You are given a string s
consisting of lowercase English letters. Your task is to count the frequency of each character in the string and then display the results sorted by the following rules:
- First, sort by frequency in descending order (i.e. the character that appears more often comes first).
- If two characters have the same frequency, sort them in alphabetical order (i.e. ascending order).
For example, given the string abbccc
, the frequencies are: a:1
, b:2
, c:3
. Thus, the output order should be: c
then b
then a
.
Note: The input will be provided via stdin
and the output should be written to stdout
in the format described below.
inputFormat
The input consists of a single line containing a string s
. The string may be empty and will only contain lowercase English letters.
Input Format:
s
outputFormat
Output the frequency of each character on a new line. Each line should contain the character and its frequency separated by a single space. The order must follow the rules: first by descending frequency and then by ascending alphabetical order if frequencies are equal. If the input string is empty, output nothing.
Output Format:
char1 freq1 char2 freq2 ...## sample
aabbcc
a 2
b 2
c 2
</p>