This is a medium question.
Fin and Jack continued their adventure in the mystical land of Wasai. After crossing the mysterious bridge, they arrived at the other side of Wasai, discovering lush green forests and crystal-clear lakes. Deep within the forest, they encountered a peculiar creature, a colorful rabbit named Lina, adorned with spots of all colors, resembling a rainbow in the sky.
They wondered if there were human settlements nearby and attempted to communicate with Lina. However, they found themselves unable to understand each other. Just as they felt puzzled, they stumbled upon an ancient and mysterious object that explained how to convert their language into one that Lina could understand through manipulation:
Given a line of text, you are required to parse it and perform the following operations:
- Example input:
hello john how are you
- Example output:
cdeeabfacgbcahbijdbkal
- After counting and sorting the characters based on their frequency and order of occurrence, we have:
- 'o', ' ' occurs 4 times
- 'h' occurs 3 times
- 'e', 'l' occurs 2 times
- 'j', 'n', 'w', 'a', 'r', 'y', and 'u' occur 1 time each
- Encoding the characters:
- 'o' -> 'a'
- ' ' -> 'b'
- 'h' -> 'c'
- 'e' -> 'd'
- 'l' -> 'e'
- 'j' -> 'f'
- 'n' -> 'g'
- 'w' -> 'h'
- 'a' -> 'i'
- 'r' -> 'j'
- 'y' -> 'k'
- 'u' -> 'l'
The input will consist of a line of text.
The output should be the encoded version of the input text, with the most frequent character replaced by 'a', the second most frequent character replaced by 'b', and so on.