-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP141.java
38 lines (33 loc) · 1.16 KB
/
P141.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
36
37
38
package p100_199;
import java.util.Scanner;
import java.util.Stack;
//accepted
public class P141 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
Stack<Character> stack = new Stack<>();
boolean valid = true;
for (int i = 0; i < s.length(); i++) {
switch (s.charAt(i)) {
case '{':
case'[':
case'(':
stack.push(s.charAt(i));
break;
case '}':
if (stack.isEmpty() || stack.pop()!='{') valid = false; break;
case ']':
if (stack.isEmpty() || stack.pop()!='[') valid = false; break;
case ')':
if (stack.isEmpty() || stack.pop()!='(') valid = false; break;
}
if (!valid) break;
}
if (!valid || !stack.isEmpty()) System.out.println("NO");
else System.out.println("YES");
stack.clear();
}
}
}