Skip to content

Commit 9e8c727

Browse files
authored
feat: add solutions to lc problems: No.1370+ (#2157)
1 parent 2110e01 commit 9e8c727

File tree

22 files changed

+700
-382
lines changed

22 files changed

+700
-382
lines changed

solution/1300-1399/1370.Increasing Decreasing String/README.md

+89-57
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

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+
7684
<!-- tabs:start -->
7785

7886
### **Python3**
@@ -82,20 +90,15 @@
8290
```python
8391
class Solution:
8492
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]
8895
ans = []
8996
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)
99102
```
100103

101104
### **Java**
@@ -105,22 +108,23 @@ class Solution:
105108
```java
106109
class Solution {
107110
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']++;
111115
}
112116
StringBuilder sb = new StringBuilder();
113-
while (sb.length() < s.length()) {
117+
while (sb.length() < n) {
114118
for (int i = 0; i < 26; ++i) {
115-
if (counter[i] > 0) {
119+
if (cnt[i] > 0) {
116120
sb.append((char) ('a' + i));
117-
--counter[i];
121+
--cnt[i];
118122
}
119123
}
120124
for (int i = 25; i >= 0; --i) {
121-
if (counter[i] > 0) {
125+
if (cnt[i] > 0) {
122126
sb.append((char) ('a' + i));
123-
--counter[i];
127+
--cnt[i];
124128
}
125129
}
126130
}
@@ -135,20 +139,22 @@ class Solution {
135139
class Solution {
136140
public:
137141
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;
141147
while (ans.size() < s.size()) {
142148
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];
146152
}
147153
}
148154
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];
152158
}
153159
}
154160
}
@@ -161,29 +167,57 @@ public:
161167
162168
```go
163169
func sortString(s string) string {
164-
counter := ['z' + 1]int{}
170+
cnt := [26]int{}
165171
for _, c := range s {
166-
counter[c]++
172+
cnt[c-'a']++
167173
}
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]--
174181
}
175182
}
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]--
180187
}
181188
}
182189
}
183190
return string(ans)
184191
}
185192
```
186193

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+
187221
### **Javascript**
188222

189223
```js
@@ -192,28 +226,26 @@ func sortString(s string) string {
192226
* @return {string}
193227
*/
194228
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)];
199232
}
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];
207239
}
208240
}
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];
213245
}
214246
}
215247
}
216-
return rs;
248+
return ans.join('');
217249
};
218250
```
219251

0 commit comments

Comments
 (0)