Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- using namespace std;
- static constexpr array<pair<int,int>, 256> init_array() {
- array<pair<int,int>,256> alphabet;
- for (int i = 0; i != 256; ++i) {
- alphabet[i].first = 0;
- alphabet[i].second = i;
- }
- return alphabet;
- }
- class Solution {
- private:
- static inline constexpr array<pair<int,int>,256> init = init_array();
- public:
- string frequencySort(string s) {
- array<pair<int,int>,256> alphabet = init;
- for (unsigned char c : s) {
- ++alphabet[c].first;
- }
- sort(alphabet.begin(), alphabet.end(), std::greater{});
- string res;
- res.reserve(s.size());
- for (int i = 0; i != 256 && alphabet[i].first != 0; ++i) {
- for (int j = 0; j != alphabet[i].first; ++j) {
- res.push_back(alphabet[i].second);
- }
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement