Skip to content

Commit 82f23c8

Browse files
author
Rishab Mehra
committed
May 22 ques
1 parent 51df38f commit 82f23c8

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3337/
2+
3+
class Solution {
4+
public:
5+
static bool sortbysecdesc (const pair<int,int> &a, const pair<int,int> &b) {
6+
return (a.second > b.second);
7+
}
8+
9+
string frequencySort(string s) {
10+
map<int, int> charFreqMap;
11+
int i;
12+
for(i = 0; i < s.size(); i++) {
13+
if(charFreqMap.find(s[i]) == charFreqMap.end())
14+
charFreqMap[s[i]] = 0;
15+
charFreqMap[s[i]]++;
16+
}
17+
vector<pair<int, int>> transformVector;
18+
19+
copy(charFreqMap.begin(), charFreqMap.end(), back_inserter(transformVector));
20+
21+
sort(transformVector.begin(), transformVector.end(), sortbysecdesc);
22+
23+
string ans = "";
24+
for(i = 0; i < transformVector.size(); i++) {
25+
pair<int, int> p = transformVector[i];
26+
for(int j = 0; j < p.second; j++)
27+
ans += p.first;
28+
}
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)