Skip to content

Commit bb86214

Browse files
add 1370
1 parent 303c648 commit bb86214

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1371|[Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1371.java) | |Medium||String
12+
|1370|[Increasing Decreasing String](https://leetcode.com/problems/increasing-decreasing-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1370.java) | |Easy||String, Sort
1213
|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1367.java) | |Medium||DP, Linked List, Tree
1314
|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1366.java) | |Medium||Array, Sort
1415
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1365.java) | |Easy||Array, HashTable
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1370. Increasing Decreasing String
5+
*
6+
* Given a string s. You should re-order the string using the following algorithm:
7+
* Pick the smallest character from s and append it to the result.
8+
* Pick the smallest character from s which is greater than the last appended character to the result and append it.
9+
* Repeat step 2 until you cannot pick more characters.
10+
* Pick the largest character from s and append it to the result.
11+
* Pick the largest character from s which is smaller than the last appended character to the result and append it.
12+
* Repeat step 5 until you cannot pick more characters.
13+
* Repeat the steps from 1 to 6 until you pick all characters from s.
14+
* In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
15+
* Return the result string after sorting s with this algorithm.
16+
*
17+
* Example 1:
18+
* Input: s = "aaaabbbbcccc"
19+
* Output: "abccbaabccba"
20+
* Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc"
21+
* After steps 4, 5 and 6 of the first iteration, result = "abccba"
22+
* First iteration is done. Now s = "aabbcc" and we go back to step 1
23+
* After steps 1, 2 and 3 of the second iteration, result = "abccbaabc"
24+
* After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"
25+
*
26+
* Example 2:
27+
* Input: s = "rat"
28+
* Output: "art"
29+
* Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm.
30+
*
31+
* Example 3:
32+
* Input: s = "leetcode"
33+
* Output: "cdelotee"
34+
*
35+
* Example 4:
36+
* Input: s = "ggggggg"
37+
* Output: "ggggggg"
38+
*
39+
* Example 5:
40+
* Input: s = "spo"
41+
* Output: "ops"
42+
*
43+
* Constraints:
44+
* 1 <= s.length <= 500
45+
* s contains only lower-case English letters.
46+
* */
47+
public class _1370 {
48+
public static class Solution1 {
49+
public String sortString(String s) {
50+
int[] count = new int[26];
51+
for (char c : s.toCharArray()) {
52+
count[c - 'a']++;
53+
}
54+
StringBuilder sb = new StringBuilder();
55+
while (sb.length() < s.length()) {
56+
for (int i = 0; i < count.length; i++) {
57+
if (count[i] != 0) {
58+
char character = (char) (i + 'a');
59+
sb.append(character);
60+
count[i]--;
61+
}
62+
}
63+
for (int i = 25; i>= 0; i--) {
64+
if (count[i] > 0) {
65+
char character = (char) (i + 'a');
66+
sb.append(character);
67+
count[i]--;
68+
}
69+
}
70+
}
71+
return sb.toString();
72+
}
73+
}
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1370;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1370Test {
10+
private static _1370.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1370.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("abccbaabccba", solution1.sortString("aaaabbbbcccc"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("art", solution1.sortString("rat"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("cdelotee", solution1.sortString("leetcode"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("ggggggg", solution1.sortString("ggggggg"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals("ops", solution1.sortString("spo"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy