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
2d

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
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter