File tree 1 file changed +8
-8
lines changed
scripts/algorithms/S/Sort Characters By Frequency
1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 22 ms (Top 36.06%) | Memory: 8.5 MB (Top 65.22%)
1
2
class Solution {
2
3
public:
3
- string frequencySort (string s)
4
+ string frequencySort (string s)
4
5
{
5
6
unordered_map<char ,int >mp;
6
- for (int i=0 ;i<s.length ();i++) // get the frequency of every char of the string
7
+ for (int i=0 ;i<s.length ();i++) // get the frequency of every char of the string
7
8
{
8
9
mp[s[i]]++;
9
10
}
10
-
11
- priority_queue<pair<int ,char >>pq; // store the freq and char pair in max heap
11
+
12
+ priority_queue<pair<int ,char >>pq; // store the freq and char pair in max heap
12
13
for (auto it=mp.begin ();it!=mp.end ();it++)
13
14
{
14
15
pq.push ({it->second ,it->first });
@@ -17,15 +18,14 @@ class Solution {
17
18
int val;
18
19
while (!pq.empty ())
19
20
{
20
- val=pq.top ().first ; // append the char frequency times
21
+ val=pq.top ().first ; // append the char frequency times
21
22
while (val--)
22
23
{
23
24
str.push_back (pq.top ().second );
24
25
}
25
26
pq.pop ();
26
27
}
27
28
return str;
28
-
29
- }
30
- };
31
29
30
+ }
31
+ };
You can’t perform that action at this time.
0 commit comments