-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStarStringProcessor.java
More file actions
32 lines (27 loc) · 921 Bytes
/
StarStringProcessor.java
File metadata and controls
32 lines (27 loc) · 921 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
package stack;
import java.util.*;
public class StarStringProcessor {
public String removeStars(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '*') {
if (!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(c);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.insert(0, stack.pop());
}
return sb.toString();
}
public static void main(String[] args) {
StarStringProcessor processor = new StarStringProcessor();
assert processor.removeStars("leet**cod*e").equals("lecoe") : "Test case 1 failed";
assert processor.removeStars("erase*****").equals("") : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}