-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincrease_decrease_string.cc
More file actions
36 lines (32 loc) · 887 Bytes
/
Copy pathincrease_decrease_string.cc
File metadata and controls
36 lines (32 loc) · 887 Bytes
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
class Solution {
public:
string sortString(string s) {
string res;
vector<int> freq(26, 0);
for (auto c : s)
freq[c - 'a']++;
bool flag = true;
while(flag) {
flag = false;
// Generate increasing string
for (int i = 0; i < 26; i++) {
if (freq[i]) {
char ch = i + 'a';
res.push_back(ch);
freq[i]--;
flag = true;
}
}
// Generate decreasing string
for (int i = 25; i >= 0; i--) {
if (freq[i]) {
char ch = i + 'a';
res.push_back(ch);
freq[i]--;
flag = true;
}
}
}
return res;
}
};