#K13266. Most Frequent Character
Most Frequent Character
Most Frequent Character
You are given a string s
. Your task is to write a program that finds the character that appears most frequently in s
. In the event that multiple characters have the same highest frequency, you should return the character that appears first in the string.
For example, consider the string aabbbccde
. The character 'b'
appears three times, which is more than any other character, so the output should be b
.
Similarly, for the input xyzabcdxyz
, although 'x'
and 'z'
both appear twice, 'x'
appears first, so your program should output x
.
Note: If the string contains only one character, simply output that character.
The problem can be formulated mathematically as follows:
Let \( f(c) \) denote the frequency of character \( c \) in s. You need to find \( c^* \) such that
[ c^* = \min{ c \mid f(c) = \max_{x \in s} f(x) } ]
where \( \min\) means the one that appears first in the string.
inputFormat
The input consists of a single line containing the string s
.
You can assume that s
is non-empty and composed of printable characters.
outputFormat
Output a single character — the most frequent character in the string according to the problem’s requirement.
## sampleaabbbccde
b