-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2182.Construct-String-With-Repeat-Limit.cpp
More file actions
39 lines (38 loc) · 1.22 KB
/
2182.Construct-String-With-Repeat-Limit.cpp
File metadata and controls
39 lines (38 loc) · 1.22 KB
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
class Solution {
public:
static bool compare(const pair<char, int>& a, const pair<char, int>& b) {
return a.first > b.first;
}
string repeatLimitedString(string s, int k) {
string ans;
unordered_map<char,int>mp;
for(int i=0;i<s.size();i++) mp[s[i]]++;
vector<pair<char, int>>vec(mp.begin(), mp.end());
sort(vec.begin(), vec.end(), compare);
int slow=0;
int fast=1;
int n=vec.size();
while(fast<n){
if(vec[slow].second<=k){
for(int i=0;i<vec[slow].second;i++) ans.push_back(vec[slow].first);
vec[slow].second=0;
slow++;
if(fast==slow) fast++;
}else{
for(int i=0;i<k;i++) ans.push_back(vec[slow].first);
vec[slow].second-=k;
if(vec[fast].second>0){
ans.push_back(vec[fast].first);
vec[fast].second--;
}
if(vec[fast].second==0) fast++;
}
}
if(vec[slow].second!=0){
for(int i=0;i<min(vec[slow].second,k);i++){
ans.push_back(vec[slow].first);
}
}
return ans;
}
};