-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1370_Increasing_Decreasing_String.cpp
More file actions
42 lines (37 loc) · 1.07 KB
/
1370_Increasing_Decreasing_String.cpp
File metadata and controls
42 lines (37 loc) · 1.07 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
40
41
42
class Solution {
public:
string sortString(string s) {
bool check = true;
string result="";
vector<int> letters(26, 0);
for(int i = 0; i < s.size(); ++i){
letters[s[i] - 'a']++;
}
for(int i = 0; i < s.size();){
if(check){
for(int j = 0; j < 26; ++j){
if(letters[j] > 0){
letters[j]--;
char c = j + 'a';
string s1(1, c);
result += s1;
++i;
}
}
}
else{
for(int j = 25; j >= 0; --j){
if(letters[j] > 0){
letters[j]--;
char c = j + 'a';
string s1(1, c);
result += s1;
++i;
}
}
}
check = !(check);
}
return result;
}
};