Advertisement

Sort Characters By Frequency - LeetCode 451 Solution

Sort Characters By Frequency - Complete Solution Guide

Sort Characters By Frequency is LeetCode problem 451, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s , sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. Return the sorted string . If there are multiple answers, return any of them . Example 1: Input: s = "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. Example 2: Input: s = "cccaaa" Output: "aaaccc" Explanatio

Detailed Explanation

The problem requires sorting a given string such that characters appear in decreasing order based on their frequency in the string. The frequency of a character is the number of times it occurs. The output should be a string where characters with higher frequencies appear earlier, and if characters have the same frequency, their order does not matter as long as they are grouped together. Uppercase and lowercase characters are treated as distinct.

Solution Approach

The solution involves three main steps: counting character frequencies, sorting the characters by frequency, and reconstructing the sorted string. A hash map (dictionary/map) is used to efficiently count the occurrences of each character. Then, the character-frequency pairs are sorted based on frequency in descending order. Finally, the sorted characters are appended to build the resulting string based on their respective frequencies.

Step-by-Step Algorithm

  1. Step 1: **Count Character Frequencies:** Iterate through the input string and store the frequency of each character in a hash map (dictionary). If a character is already present, increment its count; otherwise, add it to the map with a count of 1.
  2. Step 2: **Sort by Frequency:** Convert the hash map into a list of (character, frequency) pairs. Sort this list in descending order based on the frequency. This step leverages a sorting algorithm (or specialized data structure like a heap).
  3. Step 3: **Reconstruct Sorted String:** Iterate through the sorted list of (character, frequency) pairs. For each pair, append the character to the result string as many times as its frequency indicates.

Key Insights

  • Insight 1: Counting character frequencies is essential. A hash table (dictionary/map) is an efficient data structure for this purpose.
  • Insight 2: Sorting the characters based on their frequencies is crucial. We need a sorting algorithm or data structure that can efficiently arrange the characters by their counts.
  • Insight 3: Reconstructing the sorted string from the frequency-sorted characters.
  • Insight 4: The problem explicitly allows for multiple valid outputs, meaning that any arrangement of characters with the same frequency is acceptable.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Sorting, Heap (Priority Queue), Bucket Sort, Counting.

Companies

Asked at: Nvidia, Wipro, tcs.