Sort characters by frequency

Beginner Problems Basic Strings Easy
  • This problem is often encountered when developing data analysis tools or text editors, where understanding the frequency of character usage can be important
  • For instance, optimizing compression algorithms such as Huffman coding relies on knowing the frequency of each character in the dataset
  • This problem's concept is also used in SEO (Search Engine Optimization) analytics, where the frequency of certain words or characters can affect a webpage’s visibility in search engine results

You are given a string s. Return the array of unique characters, sorted by highest to lowest occurring characters.

If two or more characters have same frequency then arrange them in alphabetic order.

Examples:

Input : s = "tree"

Output : ['e', 'r', 't' ]

Explanation : The occurrences of each character are as shown below :

e --> 2

r --> 1

t --> 1.

The r and t have same occurrences , so we arrange them by alphabetic order.

Input : s = "raaaajj"

Output : ['a' , 'j', 'r' ]

Explanation : The occurrences of each character are as shown below :

a --> 4

j --> 2

r --> 1

Input : s = "bbccddaaa"

Constraints

  • 1 <= s.length <= 105
  • s consist of only lowercase English characters.

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree