Skip to content

Commit e22d545

Browse files
add 1544
1 parent 777f9dc commit e22d545

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1544|[Make The String Great](https://leetcode.com/problems/make-the-string-great/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1544.java) | |Easy|String, Stack|
1112
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1541.java) | |Medium|String, Stack|
1213
|1539|[Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1539.java) | |Easy|Array, HashTable|
1314
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1535.java) | [:tv:](https://youtu.be/v6On1TQfMTU) |Medium|Array|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
public class _1544 {
6+
public static class Solution1 {
7+
public String makeGood(String s) {
8+
Stack<Character> stack = new Stack<>();
9+
for (int i = 0; i < s.length() - 1; i++) {
10+
char c = s.charAt(i);
11+
if (stack.isEmpty()) {
12+
stack.add(c);
13+
} else {
14+
if (Character.toLowerCase(stack.peek()) == Character.toLowerCase(c)) {
15+
if ((Character.isLowerCase(stack.peek()) && Character.isUpperCase(c))) {
16+
stack.pop();
17+
} else if (Character.isUpperCase(stack.peek()) && Character.isLowerCase(c)) {
18+
stack.pop();
19+
} else {
20+
stack.add(c);
21+
}
22+
} else {
23+
stack.add(c);
24+
}
25+
}
26+
}
27+
char c = s.charAt(s.length() - 1);
28+
if (!stack.isEmpty() && Character.toLowerCase(stack.peek()) == Character.toLowerCase(c)) {
29+
if ((Character.isLowerCase(stack.peek()) && Character.isUpperCase(c))) {
30+
stack.pop();
31+
} else if (Character.isUpperCase(stack.peek()) && Character.isLowerCase(c)) {
32+
stack.pop();
33+
} else {
34+
stack.add(c);
35+
}
36+
} else {
37+
stack.add(c);
38+
}
39+
StringBuilder sb = new StringBuilder();
40+
while (!stack.isEmpty()) {
41+
sb.append(stack.pop());
42+
}
43+
return sb.reverse().toString();
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1544;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1544Test {
10+
private static _1544.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1544.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("leetcode", solution1.makeGood("leEeetcode"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("", solution1.makeGood("abBAcC"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("s", solution1.makeGood("s"));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)