-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1370-increasing-decreasing-string.js
50 lines (47 loc) · 1.55 KB
/
1370-increasing-decreasing-string.js
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
/**
* 1370. Increasing Decreasing String
* https://leetcode.com/problems/increasing-decreasing-string/
* Difficulty: Easy
*
* You are given a string s. Reorder the string using the following algorithm:
* 1. Remove the smallest character from s and append it to the result.
* 2. Remove the smallest character from s that is greater than the last appended character,
* and append it to the result.
* 3. Repeat step 2 until no more characters can be removed.
* 4. Remove the largest character from s and append it to the result.
* 5. Remove the largest character from s that is smaller than the last appended character,
* and append it to the result.
* 6. Repeat step 5 until no more characters can be removed.
* 7. Repeat steps 1 through 6 until all characters from s have been removed.
*
* If the smallest or largest character appears more than once, you may choose any occurrence to
* append to the result.
*
* Return the resulting string after reordering s using this algorithm.
*/
/**
* @param {string} s
* @return {string}
*/
var sortString = function(s) {
const charCounts = new Array(26).fill(0);
for (const char of s) {
charCounts[char.charCodeAt(0) - 97]++;
}
let result = '';
while (result.length < s.length) {
for (let i = 0; i < 26; i++) {
if (charCounts[i] > 0) {
result += String.fromCharCode(i + 97);
charCounts[i]--;
}
}
for (let i = 25; i >= 0; i--) {
if (charCounts[i] > 0) {
result += String.fromCharCode(i + 97);
charCounts[i]--;
}
}
}
return result;
};