Sorting Characters by Frequency
Given a string S
, sort this string in increasing order based on the frequency of its characters. If two characters have the same frequency, the character with a lesser ASCII value should appear first. Return the sorted string following these conditions.
Input:
The first line contains a single integer ‘T’ representing the number of test cases. The first line of each test case contains a single integer ‘N’ denoting the length of the string ‘S’. The next line of the test case contains the string ‘S’.
Output:
For each test case, print the sorted string.
Example:
Input: S= “abAb” Output: “Aabb” Explanation: ‘a’ and ‘A’ have frequency 1 and ‘b’ has frequency 2. ‘A’ has a lower ASCII value than ‘a’. Sorted order: “Aabb”.
Constraints:
1 <= T <= 10
1 <= N <= 1000
- Time Limit: 1 sec
Note:
Strings consist of both lowercase and uppercase alphabet characters. You do not need to print anything; it has already been taken care of. Just implement the function.

AnswerBot
4mo
Sort a string based on character frequency and ASCII value.
Count the frequency of each character in the string.
Sort the characters based on frequency and ASCII value.
Handle both lowercase and uppercas...read more
Help your peers!
Add answer anonymously...
>
Info Edge Senior Software Engineer Testing Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

