BackmediumStrings Accenture PhonePe

Galactic Transmission Optimization Solution

Problem Statement

Given a transmission message, find the frequency of each character and then reorder the message based on the frequency of characters in descending order. If two characters have the same frequency, the character with the smaller ASCII value will come first.

Example 1
Input
abcabc
Output
acbabcc

Explanation: Step-by-step: First, we count the frequency of each character in the input string 'abcabc'. The frequency of 'a' is 3, 'b' is 2, and 'c' is 2. Then, we sort the characters based on their frequency in descending order and ASCII value in ascending order. Since 'a' has a higher frequency than 'b' and 'c', it comes first. Among 'b' and 'c', 'b' has a smaller ASCII value, so it comes before 'c'. Therefore, the sorted string is 'acbabcc'.

Example 2
Input
zzzww
Output
zzzww

Explanation: Step-by-step: First, we count the frequency of each character in the input string 'zzzww'. The frequency of 'z' is 3, 'w' is 1. Then, we sort the characters based on their frequency in descending order and ASCII value in ascending order. Since 'z' has a higher frequency than 'w', it comes first. Therefore, the sorted string is 'zzzww'.

Constraints

  • The length of the transmission message will not exceed 1000 characters.
  • The transmission message will only contain lowercase English letters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free