forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1544.java
34 lines (32 loc) · 1.17 KB
/
_1544.java
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
package com.fishercoder.solutions;
import java.util.Stack;
public class _1544 {
public static class Solution1 {
public String makeGood(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (stack.isEmpty()) {
stack.add(c);
} else {
if (Character.toLowerCase(stack.peek()) == Character.toLowerCase(c)) {
if ((Character.isLowerCase(stack.peek()) && Character.isUpperCase(c))) {
stack.pop();
} else if (Character.isUpperCase(stack.peek()) && Character.isLowerCase(c)) {
stack.pop();
} else {
stack.add(c);
}
} else {
stack.add(c);
}
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.reverse().toString();
}
}
}