#C10780. Most Frequent Character

    ID: 40023 Type: Default 1000ms 256MiB

Most Frequent Character

Most Frequent Character

You are given a string s. Your task is to determine the character that appears most frequently in s. In case of a tie where multiple characters occur the same maximum number of times, you should output the lexicographically smallest character.

Note: The lexicographical order is based on the standard character ordering (i.e. according to their ASCII/Unicode values).

For example:

  • If s = "a", the answer is a.
  • If s = "bbbb", the answer is b.
  • If s = "abracadabra", although both 'a' and 'b' might be candidates, the answer is a because it is lexicographically smaller.

Solve the problem by reading the input from standard input (stdin) and writing the result to standard output (stdout).

The frequency calculation can be mathematically expressed as follows:

$$\text{Let } f(c) = \text{number of occurrences of character } c \text{ in } s. $$$$\text{We want to find } c^* \text{ such that } f(c^*) = \max_{c \in s} f(c) \text{ and for any } d \text{ with } f(d) = f(c^*),\ c^* \leq d. $$

inputFormat

The input consists of a single line containing the string s. The string may include spaces and special characters.

outputFormat

Output a single character which is the most frequent character in the input string. If there is a tie, output the lexicographically smallest character among those with the highest frequency.

## sample
a
a