-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3174.java
35 lines (33 loc) · 1.25 KB
/
_3174.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
35
package com.fishercoder.solutions.fourththousand;
import java.util.Deque;
import java.util.LinkedList;
public class _3174 {
public static class Solution1 {
public String clearDigits(String s) {
Deque<Character> stack = new LinkedList<>();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
if (!stack.isEmpty()) {
Deque<Character> temp = new LinkedList<>();
while (!stack.isEmpty() && Character.isDigit(stack.peekLast())) {
temp.addLast(stack.pollLast());
}
if (!stack.isEmpty() && !Character.isDigit(stack.peekLast())) {
stack.pollLast();
while (!temp.isEmpty()) {
stack.addLast(temp.pollLast());
}
}
}
} else {
stack.addLast(c);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pollLast());
}
return sb.reverse().toString();
}
}
}