#C13516. Group and Sort Characters by Frequency

    ID: 43063 Type: Default 1000ms 256MiB

Group and Sort Characters by Frequency

Group and Sort Characters by Frequency

Given an input string, your task is to group its characters based on their frequencies and then sort them using the following rules:

  • Characters with higher frequency should appear before those with lower frequency.
  • If two characters share the same frequency, they should be arranged in ascending alphabetical order.

Mathematically, if f(x) denotes the frequency of character \(x\) in the input string, the ordering should satisfy:

\[ \text{For any two characters } x \text{ and } y, \; f(x) > f(y) \text{ or (if } f(x)=f(y) \text{ then } x < y) \]

For example:

  • Input: tree → Output: eert
  • Input: cccaaa → Output: aaaccc
  • Input: Aabb → Output: bbAa

You are required to read the input from standard input (stdin) and output the result to standard output (stdout).

inputFormat

The input consists of a single line containing a string s. The string may be empty.

outputFormat

Output a single line containing the rearranged string after grouping and sorting the characters as per the described rules.

## sample
tree
eert

</p>