12805 - GEC1506 - Advanced - Decoding   

Description

 

In this problem, you will learn how to encode and decode text using a frequency-based encoding rule. You will first create an encoding scheme based on the frequency of characters in a given text, and then use this scheme to decode a given encoded message.

Note:

  1. Characters should be converted to lower-case using the built-in function .lower().
  2. Consider spaces (' ') when counting character frequencies.

Input

 

  • A string text (1 <= len(text) <= 10^4): represents the text to learn the encoding rule.
  • A string message (1 <= len(message) <= 10^5): represents the message to be decoded.

 

Example Input:

Hello john

000001001100000000001100010101001

 
 

Output

 

Decode the encoded message using the encoding scheme generated from the first line of the input text.

 

Encoding Rule:

  1. Count the frequency of each character in the text.
  2. Sort the characters in descending order based on their frequency. If multiple characters have the same frequency, maintain their order of occurrence.
  3. Encode the most frequent character with '1'.
  4. For each subsequent character, add a '0' to the left of the previous character's encoding.
  5. For the least frequent character, encode it with the same length as the previous character but with all digits turned to '0'.

 

Example Encoding:

'h': 2 times -> '1'
'l': 2 times -> '01'
'o': 2 times -> '001'
'e': 1 time -> '0001'
' ': 1 time -> '00001'
'j': 1 time -> '000001'
'n': 1 time -> '000000'

 

Example Output:

john hello

 

Sample Input  Download

Sample Output  Download

Tags




Discuss