-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgss2.java
More file actions
38 lines (32 loc) · 916 Bytes
/
Pgss2.java
File metadata and controls
38 lines (32 loc) · 916 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
37
38
package linear;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class Pgss2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String result = pre(input);
System.out.println(result);
}
public static String pre(String str) {
Deque<Character> stack = new ArrayDeque<>();
int count = 0;
for (char ch : str.toCharArray()) {
if (ch == '(') {
count++;
} else if (ch == ')') {
count--;
} else {
if (count == 0) {
stack.addLast(ch);
}
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.removeFirst());
}
return sb.toString();
}
}