14335 - Medium - Wasai Language Converter   

Description

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:

  1. Count the frequency of each character in the text (including spaces).
  2. If multiple characters have the same frequency, order them according to their first occurrence in the text.
  3. Encode the characters according to their frequency, replacing each character with a corresponding lowercase letter from the alphabet (a-z) in alphabetical order. 
  • 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'

Input

The input will consist of a line of text.

  • 1 <= len(text) <= 10^4
  • The text will only contain lowercase alphabetical characters and spaces.
  • The number of character types in each input will not exceed 26.
 

Output

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.​

Sample Input  Download

Sample Output  Download

Tags




Discuss