73
73
74
74
<!-- 这里可写通用的实现逻辑 -->
75
75
76
+ ** 方法一:计数 + 模拟**
77
+
78
+ 我们先用一个哈希表或者一个长度为 $26$ 的数组 $cnt$ 统计字符串 $s$ 中每个字符出现的次数。
79
+
80
+ 然后,我们枚举字母 $[ a,...,z] $,对于当前枚举到的字母 $c$,如果 $cnt[ c] \gt 0$,我们就将字母 $c$ 接在答案字符串的末尾,并将 $cnt[ c] $ 减一。我们重复这一步骤,直到 $cnt[ c] =0$。随后我们逆序枚举字母 $[ z,...,a] $,执行类似的操作。如果答案字符串的长度等于 $s$ 的长度,那么我们就完成了所有的拼接操作。
81
+
82
+ 时间复杂度 $O(n \times |\Sigma|)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $s$ 的长度,而 $\Sigma$ 是字符集,本题中字符集为所有小写字母,因此 $|\Sigma|=26$。
83
+
76
84
<!-- tabs:start -->
77
85
78
86
### ** Python3**
82
90
``` python
83
91
class Solution :
84
92
def sortString (self , s : str ) -> str :
85
- counter = [0 ] * 26
86
- for c in s:
87
- counter[ord (c) - ord (' a' )] += 1
93
+ cnt = Counter(s)
94
+ cs = ascii_lowercase + ascii_lowercase[::- 1 ]
88
95
ans = []
89
96
while len (ans) < len (s):
90
- for i in range (26 ):
91
- if counter[i]:
92
- ans.append(chr (i + ord (' a' )))
93
- counter[i] -= 1
94
- for i in range (25 , - 1 , - 1 ):
95
- if counter[i]:
96
- ans.append(chr (i + ord (' a' )))
97
- counter[i] -= 1
98
- return ' ' .join(ans)
97
+ for c in cs:
98
+ if cnt[c]:
99
+ ans.append(c)
100
+ cnt[c] -= 1
101
+ return " " .join(ans)
99
102
```
100
103
101
104
### ** Java**
@@ -105,22 +108,23 @@ class Solution:
105
108
``` java
106
109
class Solution {
107
110
public String sortString (String s ) {
108
- int [] counter = new int [26 ];
109
- for (char c : s. toCharArray()) {
110
- ++ counter[c - ' a' ];
111
+ int [] cnt = new int [26 ];
112
+ int n = s. length();
113
+ for (int i = 0 ; i < n; ++ i) {
114
+ cnt[s. charAt(i) - ' a' ]++ ;
111
115
}
112
116
StringBuilder sb = new StringBuilder ();
113
- while (sb. length() < s . length() ) {
117
+ while (sb. length() < n ) {
114
118
for (int i = 0 ; i < 26 ; ++ i) {
115
- if (counter [i] > 0 ) {
119
+ if (cnt [i] > 0 ) {
116
120
sb. append((char ) (' a' + i));
117
- -- counter [i];
121
+ -- cnt [i];
118
122
}
119
123
}
120
124
for (int i = 25 ; i >= 0 ; -- i) {
121
- if (counter [i] > 0 ) {
125
+ if (cnt [i] > 0 ) {
122
126
sb. append((char ) (' a' + i));
123
- -- counter [i];
127
+ -- cnt [i];
124
128
}
125
129
}
126
130
}
@@ -135,20 +139,22 @@ class Solution {
135
139
class Solution {
136
140
public:
137
141
string sortString(string s) {
138
- vector<int > counter(26);
139
- for (char c : s) ++counter[ c - 'a'] ;
140
- string ans = "";
142
+ int cnt[ 26] {};
143
+ for (char& c : s) {
144
+ ++cnt[ c - 'a'] ;
145
+ }
146
+ string ans;
141
147
while (ans.size() < s.size()) {
142
148
for (int i = 0; i < 26; ++i) {
143
- if (counter [ i] ) {
144
- ans += ( i + 'a') ;
145
- --counter [ i] ;
149
+ if (cnt [ i] ) {
150
+ ans += i + 'a';
151
+ --cnt [ i] ;
146
152
}
147
153
}
148
154
for (int i = 25; i >= 0; --i) {
149
- if (counter [ i] ) {
150
- ans += ( i + 'a') ;
151
- --counter [ i] ;
155
+ if (cnt [ i] ) {
156
+ ans += i + 'a';
157
+ --cnt [ i] ;
152
158
}
153
159
}
154
160
}
@@ -161,29 +167,57 @@ public:
161
167
162
168
```go
163
169
func sortString(s string) string {
164
- counter := ['z' + 1 ]int{}
170
+ cnt := [26 ]int{}
165
171
for _, c := range s {
166
- counter[c ]++
172
+ cnt[c-'a' ]++
167
173
}
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
+ n := len(s)
175
+ ans := make([]byte, 0, n)
176
+ for len(ans) < n {
177
+ for i := 0; i < 26; i++ {
178
+ if cnt[i] > 0 {
179
+ ans = append(ans, byte(i)+'a')
180
+ cnt[i]--
174
181
}
175
182
}
176
- for i := byte('z') ; i >= 'a' ; i-- {
177
- if counter [i] > 0 {
178
- ans = append(ans, i )
179
- counter [i]--
183
+ for i := 25 ; i >= 0 ; i-- {
184
+ if cnt [i] > 0 {
185
+ ans = append(ans, byte(i)+'a' )
186
+ cnt [i]--
180
187
}
181
188
}
182
189
}
183
190
return string(ans)
184
191
}
185
192
```
186
193
194
+ ### ** TypeScript**
195
+
196
+ ``` ts
197
+ function sortString(s : string ): string {
198
+ const cnt: number [] = Array (26 ).fill (0 );
199
+ for (const c of s ) {
200
+ ++ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
201
+ }
202
+ const ans: string [] = [];
203
+ while (ans .length < s .length ) {
204
+ for (let i = 0 ; i < 26 ; ++ i ) {
205
+ if (cnt [i ]) {
206
+ ans .push (String .fromCharCode (i + ' a' .charCodeAt (0 )));
207
+ -- cnt [i ];
208
+ }
209
+ }
210
+ for (let i = 25 ; i >= 0 ; -- i ) {
211
+ if (cnt [i ]) {
212
+ ans .push (String .fromCharCode (i + ' a' .charCodeAt (0 )));
213
+ -- cnt [i ];
214
+ }
215
+ }
216
+ }
217
+ return ans .join (' ' );
218
+ }
219
+ ```
220
+
187
221
### ** Javascript**
188
222
189
223
``` js
@@ -192,28 +226,26 @@ func sortString(s string) string {
192
226
* @return {string}
193
227
*/
194
228
var sortString = function (s ) {
195
- let rs = ' ' ;
196
- const m = new Map ();
197
- for (let i = 0 ; i < s .length ; i++ ) {
198
- m .set (s[i], (m .get (s[i]) || 0 ) + 1 );
229
+ const cnt = Array (26 ).fill (0 );
230
+ for (const c of s) {
231
+ ++ cnt[c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
199
232
}
200
- const keys = [... m .keys ()];
201
- keys .sort ();
202
- while (rs .length < s .length ) {
203
- for (let j = 0 ; j < keys .length ; j++ ) {
204
- if (m .get (keys[j]) != 0 ) {
205
- rs += keys[j];
206
- m .set (keys[j], m .get (keys[j]) - 1 );
233
+ const ans = [];
234
+ while (ans .length < s .length ) {
235
+ for (let i = 0 ; i < 26 ; ++ i) {
236
+ if (cnt[i]) {
237
+ ans .push (String .fromCharCode (i + ' a' .charCodeAt (0 )));
238
+ -- cnt[i];
207
239
}
208
240
}
209
- for (let j = keys . length - 1 ; j >= 0 ; j -- ) {
210
- if (m . get (keys[j]) != 0 ) {
211
- rs += keys[j] ;
212
- m . set (keys[j], m . get (keys[j]) - 1 ) ;
241
+ for (let i = 25 ; i >= 0 ; -- i ) {
242
+ if (cnt[i] ) {
243
+ ans . push ( String . fromCharCode (i + ' a ' . charCodeAt ( 0 ))) ;
244
+ -- cnt[i] ;
213
245
}
214
246
}
215
247
}
216
- return rs ;
248
+ return ans . join ( ' ' ) ;
217
249
};
218
250
```
219
251
0 commit comments