[LeetCode] Majority Frequency Characters

3692. Majority Frequency Characters

You are given a string s consisting of lowercase English letters.

The frequency group for a value k is the set of characters that appear exactly k times in s.

The majority frequency group is the frequency group that contains the largest number of distinct characters.

Return a string containing all characters in the majority frequency group, in any order. If two or more frequency groups tie for that largest size, pick the group whose frequency k is larger.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
string majorityFrequencyGroup(string s) {
unordered_map<char, int> freq;
for(auto& ch : s) freq[ch]++;
unordered_map<int, string> rfreq{{0,""}};
for(auto& [k,v] : freq) rfreq[v].push_back(k);
int best = 0;
for(auto& [cnt, str] : rfreq) {
if(str.size() > rfreq[best].size()) best = cnt;
else if(str.size() == rfreq[best].size()) best = max(best, cnt);
}
return rfreq[best];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/10/11/PS/LeetCode/majority-frequency-characters/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.