-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path451_sort_chars_freq.cpp
64 lines (55 loc) · 1.3 KB
/
451_sort_chars_freq.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//LC - 451. Sort Characters By Frequency
//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.
//TC = O(nlogn) & SC = O(n)
#include <bits/stdc++.h>
using namespace std;
string frequencySort(string s){
unordered_map<char, int> mpp;
for(int i =0; i<s.length(); i++){
mpp[s[i]]++;
}
//lambda function (inline function)
auto compare = [&](char a, char b) {
if(mpp[a] != mpp[b]) {
if(mpp[a] > mpp[b]) {
return true;
}
}
else {
if(a>b) {
return true;
}
}
return false;
};
sort(s.begin(), s.end(), compare);
return s;
}
/*
takes a lot of space and time
TC= O(n * logm) & SC= O(n)
string frequencySort(string s) {
unordered_map<char, int> mpp;
for(int i =0; i<s.length(); i++){
mpp[s[i]]++;
}
vector<pair<int, char>> temp;
for(auto itr: mpp) {
temp.push_back(make_pair(itr.second, itr.first));
}
sort(temp.begin(), temp.end(), greater<pair<int, char>>());
string ans;
for(auto itr: temp){
for(int j =0; j<itr.first; j++){
ans = ans + itr.second;
}
}
return ans;
}
*/
int main () {
string s;
cin>>s;
cout<< frequencySort(s);
return 0;
}