-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode2390.java
More file actions
34 lines (30 loc) · 917 Bytes
/
LeetCode2390.java
File metadata and controls
34 lines (30 loc) · 917 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
import java.util.ArrayDeque;
import java.util.Deque;
public class LeetCode2390 {
public static void main(String[] args) {
// 输入:s = "leet**cod*e"
// 输出:"lecoe"
System.out.println(new Solution2390().removeStars("leet**cod*e"));
// 输入:s = "erase*****"
// 输出:""
// System.out.println(new Solution2390().removeStars("erase*****"));
}
}
class Solution2390 {
public String removeStars(String s) {
Deque<Character> stack = new ArrayDeque<Character>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '*') {
stack.pop();
} else {
stack.push(c);
}
}
StringBuilder sb = new StringBuilder();
for (char c : stack) {
sb.append(c);
}
return sb.reverse().toString();
}
}