Skip to main content

0451 - Sort Characters By Frequency (Medium)

https://leetcode.com/problems/sort-characters-by-frequency/

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"
Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:

Input: s = "Aabb"
Output: "bbAa"
Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.

Constraints:

  • 1 <= s.length <= 5 * 10^5
  • s consists of uppercase and lowercase English letters and digits.

Approach 1: Frequency Counting

We do the frequency count first and then use this count for a custom sort function.

Let nn the length of the input string then the

  • Time complexity is O(nlogn)O(n \log n) for the sorting, and the
  • Space complexity is O(1)O(1) as we are rewriting the input.
Written by @heder
static string frequencySort(string& s) noexcept {
array<int, 128> cnt = {};
for (char ch : s) ++cnt[ch];
sort(begin(s), end(s), [&](char a, char b) {
if (cnt[a] == cnt[b]) return a < b;
return cnt[a] > cnt[b];
});
return s;
}

Approach 2: Count, Sort, Reconstruct

This approach first does a frequency count, sorts the frequency count descending, and finally reconstructs the answer from the sorted frequency count.

Let nn the length of the input string then the

  • Time complexity is O(n)O(n), because while sorting is O(nlogn)O(n \log n) in general we only sort a limited size frequency count vector. Doing the frequency count and reconstructing the string is O(n)O(n), and the
  • Space complexity is O(n)O(n) for the output (or O(1)O(1) when we rewrite the input).
Written by @heder
static string frequencySort(const string& s) noexcept {
array<int, 128> counts = {};
for (char ch : s) ++counts[ch];
vector<pair<int, char>> sc;
sc.reserve(128);
for (int i = 0; i < size(counts); ++i)
if (counts[i]) sc.push_back({counts[i], i});
sort(begin(sc), end(sc), greater<>());
string ans;
ans.reserve(size(s));
for (auto [count, ch] : sc)
ans += string(count, ch);
return ans;
}

If we are willing to rewrite the input, then we could do something like this:

Written by @heder
static string frequencySort(string& s) noexcept {
array<int, 128> counts = {};
for (char ch : s) ++counts[ch];
vector<pair<int, char>> sc;
sc.reserve(128);
for (int i = 0; i < size(counts); ++i)
if (counts[i]) sc.push_back({counts[i], i});
sort(begin(sc), end(sc), greater<>());
s.resize(0);
for (auto [count, ch] : sc)
s += string(count, ch);
return s;
}