-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncreasing Decreasing String.cpp
95 lines (83 loc) · 2.93 KB
/
Increasing Decreasing String.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Solution {
public:
string sortString(string s) {
string res = "";
char prev;
vector<int>count(26,0);
for(auto x: s) {
count[x-'a']++;
}
int len = s.length();
int stage = 0;
while(len) {
if(stage == 0) {
// finding the least
for(int j=0 ; j<26 ; j++) {
if(count[j] > 0) {
res += j+'a';
prev = j;
stage = 1;
count[j]--;
len--;
break;
}
}
} else if(stage == 1) {
// find the next smallest letter
int f = 0;
for(int j=prev+1 ; j<26 ; j++) {
if(count[j] > 0) {
count[j]--;
res += j+'a';
f = 1;
prev = j;
len--;
break;
}
}
if(f == 0) {
stage = 2;
}
} else if(stage == 2) {
// find the largest
for(int j=25 ; j>=0 ; j--) {
if(count[j] > 0) {
count[j]--;
res+=j+'a';
prev = j;
len--;
stage = 3;
break;
}
}
} else if(stage == 3) {
int f = 0;
for(int j=prev-1 ; j>=0 ; j--) {
if(count[j]>0) {
count[j]--;
res += j+'a';
f = 1;
len--;
prev = j;
break;
}
}
if(f == 0){
stage = 0;
}
}
}
return res;
}
};
// LEETCODE 1370
// You are given a string s. Reorder the string using the following algorithm:
// 1. Pick the smallest character from s and append it to the result.
// 2. Pick the smallest character from s which is greater than the last appended character to the result and append it.
// 3. Repeat step 2 until you cannot pick more characters.
// 4. Pick the largest character from s and append it to the result.
// 5. Pick the largest character from s which is smaller than the last appended character to the result and append it.
// 6. Repeat step 5 until you cannot pick more characters.
// 7. Repeat the steps from 1 to 6 until you pick all characters from s.
// In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
// Return the result string after sorting s with this algorithm.