Skip to content

Commit 8374ddd

Browse files
committed
Add solution #1370
1 parent 2ef589c commit 8374ddd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,258 LeetCode solutions in JavaScript
1+
# 1,259 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1042,6 +1042,7 @@
10421042
1366|[Rank Teams by Votes](./solutions/1366-rank-teams-by-votes.js)|Medium|
10431043
1367|[Linked List in Binary Tree](./solutions/1367-linked-list-in-binary-tree.js)|Medium|
10441044
1368|[Minimum Cost to Make at Least One Valid Path in a Grid](./solutions/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js)|Hard|
1045+
1370|[Increasing Decreasing String](./solutions/1370-increasing-decreasing-string.js)|Easy|
10451046
1372|[Longest ZigZag Path in a Binary Tree](./solutions/1372-longest-zigzag-path-in-a-binary-tree.js)|Medium|
10461047
1374|[Generate a String With Characters That Have Odd Counts](./solutions/1374-generate-a-string-with-characters-that-have-odd-counts.js)|Easy|
10471048
1380|[Lucky Numbers in a Matrix](./solutions/1380-lucky-numbers-in-a-matrix.js)|Easy|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 1370. Increasing Decreasing String
3+
* https://leetcode.com/problems/increasing-decreasing-string/
4+
* Difficulty: Easy
5+
*
6+
* You are given a string s. Reorder the string using the following algorithm:
7+
* 1. Remove the smallest character from s and append it to the result.
8+
* 2. Remove the smallest character from s that is greater than the last appended character,
9+
* and append it to the result.
10+
* 3. Repeat step 2 until no more characters can be removed.
11+
* 4. Remove the largest character from s and append it to the result.
12+
* 5. Remove the largest character from s that is smaller than the last appended character,
13+
* and append it to the result.
14+
* 6. Repeat step 5 until no more characters can be removed.
15+
* 7. Repeat steps 1 through 6 until all characters from s have been removed.
16+
*
17+
* If the smallest or largest character appears more than once, you may choose any occurrence to
18+
* append to the result.
19+
*
20+
* Return the resulting string after reordering s using this algorithm.
21+
*/
22+
23+
/**
24+
* @param {string} s
25+
* @return {string}
26+
*/
27+
var sortString = function(s) {
28+
const charCounts = new Array(26).fill(0);
29+
for (const char of s) {
30+
charCounts[char.charCodeAt(0) - 97]++;
31+
}
32+
33+
let result = '';
34+
while (result.length < s.length) {
35+
for (let i = 0; i < 26; i++) {
36+
if (charCounts[i] > 0) {
37+
result += String.fromCharCode(i + 97);
38+
charCounts[i]--;
39+
}
40+
}
41+
for (let i = 25; i >= 0; i--) {
42+
if (charCounts[i] > 0) {
43+
result += String.fromCharCode(i + 97);
44+
charCounts[i]--;
45+
}
46+
}
47+
}
48+
49+
return result;
50+
};

0 commit comments

Comments
 (0)