@@ -70,21 +70,118 @@ After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"
70
70
<li><code>s</code> contains only lower-case English letters.</li>
71
71
</ul >
72
72
73
-
74
73
## Solutions
75
74
76
75
<!-- tabs:start -->
77
76
78
77
### ** Python3**
79
78
80
79
``` python
81
-
80
+ class Solution :
81
+ def sortString (self , s : str ) -> str :
82
+ counter = [0 ] * 26
83
+ for c in s:
84
+ counter[ord (c) - ord (' a' )] += 1
85
+ ans = []
86
+ while len (ans) < len (s):
87
+ for i in range (26 ):
88
+ if counter[i]:
89
+ ans.append(chr (i + ord (' a' )))
90
+ counter[i] -= 1
91
+ for i in range (25 , - 1 , - 1 ):
92
+ if counter[i]:
93
+ ans.append(chr (i + ord (' a' )))
94
+ counter[i] -= 1
95
+ return ' ' .join(ans)
82
96
```
83
97
84
98
### ** Java**
85
99
86
100
``` java
101
+ class Solution {
102
+ public String sortString (String s ) {
103
+ int [] counter = new int [26 ];
104
+ for (char c : s. toCharArray()) {
105
+ ++ counter[c - ' a' ];
106
+ }
107
+ StringBuilder sb = new StringBuilder ();
108
+ while (sb. length() < s. length()) {
109
+ for (int i = 0 ; i < 26 ; ++ i) {
110
+ if (counter[i] > 0 ) {
111
+ sb. append((char ) (' a' + i));
112
+ -- counter[i];
113
+ }
114
+ }
115
+ for (int i = 25 ; i >= 0 ; -- i) {
116
+ if (counter[i] > 0 ) {
117
+ sb. append((char ) (' a' + i));
118
+ -- counter[i];
119
+ }
120
+ }
121
+ }
122
+ return sb. toString();
123
+ }
124
+ }
125
+ ```
126
+
127
+ ### ** C++**
128
+
129
+ ``` cpp
130
+ class Solution {
131
+ public:
132
+ string sortString(string s) {
133
+ vector<int > counter(26);
134
+ for (char c : s) ++counter[ c - 'a'] ;
135
+ string ans = "";
136
+ while (ans.size() < s.size())
137
+ {
138
+ for (int i = 0; i < 26; ++i)
139
+ {
140
+ if (counter[ i] )
141
+ {
142
+ ans += (i + 'a');
143
+ --counter[ i] ;
144
+ }
145
+ }
146
+ for (int i = 25; i >= 0; --i)
147
+ {
148
+ if (counter[ i] )
149
+ {
150
+ ans += (i + 'a');
151
+ --counter[ i] ;
152
+ }
153
+ }
154
+ }
155
+ return ans;
156
+ }
157
+ };
158
+ ```
87
159
160
+ ### **Go**
161
+
162
+ ```go
163
+ func sortString(s string) string {
164
+ counter := ['z' + 1]int{}
165
+ for _, c := range s {
166
+ counter[c]++
167
+ }
168
+ var ans []byte
169
+ for len(ans) < len(s) {
170
+ for i := byte('a'); i <= 'z'; i++ {
171
+ if counter[i] > 0 {
172
+ ans = append(ans, i)
173
+ counter[i]--
174
+ }
175
+ }
176
+ for i := byte('z'); i >= 'a'; i-- {
177
+ if counter[i] > 0 {
178
+ ans = append(ans, i)
179
+ counter[i]--
180
+ }
181
+ }
182
+ }
183
+ return string(ans)
184
+ }
88
185
```
89
186
90
187
### ** ...**
0 commit comments