-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeStringGreat.java
More file actions
36 lines (27 loc) · 847 Bytes
/
Copy pathMakeStringGreat.java
File metadata and controls
36 lines (27 loc) · 847 Bytes
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
package LeetcodePractice;
import java.util.Stack;
public class MakeStringGreat {
public static String makeGood(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (!stack.isEmpty() && Math.abs(stack.peek() - c) == 32) {
stack.pop();
} else {
stack.push(c);
}
}
StringBuilder result = new StringBuilder();
for (char c : stack) {
result.append(c);
}
return result.toString();
}
public static void main(String[] args) {
String s1 = "leEeetcode";
System.out.println(makeGood(s1));
String s2 = "abBAcC";
System.out.println(makeGood(s2));
String s3 = "s";
System.out.println(makeGood(s3));
}
}