aboutsummaryrefslogtreecommitdiff
path: root/sort_char_freq.cpp
blob: ea7d8afb2234f6d17d3701b26b1ad66b1b871dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "leetcode.h"

class Solution {
public:
  string frequencySort(string s) {
    unordered_map<char, int> freq;
    for (auto a : s)
      freq[a]++;
    priority_queue<pair<int, char>> pqp;
    for (auto [a, f] : freq)
      pqp.push({f, a});
    string ans;
    pair<int, char> curr;
    while (!pqp.empty()) {
      curr = pqp.top();
      pqp.pop();
      ans += string(curr.first, curr.second);
    }
    return ans;
  }
};

int main() {
  Solution obj;
  cout << obj.frequencySort("tree");
}