#C13028. Most Frequent Characters
Most Frequent Characters
Most Frequent Characters
You are given a string. Your task is to find the most frequently occurring characters in the string along with their frequencies. The results should be sorted primarily in descending order of frequency, and in the case of ties, sorted in alphabetical order.
If the input is not a valid string (i.e. not enclosed in double quotes when provided as JSON), output the exact error message: Error: Input must be a string
. If the string is empty, output an empty list.
Note: The output must be printed as a list of tuples in the following format: [('a', 4), ('b', 2), ...]
.
The problem involves counting character frequencies and sorting according to the rules described using standard input (stdin) and output (stdout).
inputFormat
The input is read from standard input (stdin) as a JSON representation of a string. For example: "abracadabra"
. The input must start and end with double quotes. If it does not, consider it an invalid input.
outputFormat
Output the result to standard output (stdout) as a list of tuples. Each tuple contains a character and its frequency. The list should be sorted in descending order of frequency, with alphabetical order applied for ties. In case the input is not a valid string, output Error: Input must be a string
.
"abracadabra"
[('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]