-
Notifications
You must be signed in to change notification settings - Fork 0
/
p141.java
64 lines (62 loc) · 1.92 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.caiomed03.aceptaelreto;
import java.util.Scanner;
import java.util.Stack;
public class p141 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
boolean flag = true;
Stack<Character> a = new Stack<>();
String s1 = sc.nextLine();
for (int i = 0; i < s1.length(); i++) {
if(s1.charAt(i)=='(' || s1.charAt(i)=='[' || s1.charAt(i)=='{'){
a.push(s1.charAt(i));
}
else if(s1.charAt(i)==')'){
if(a.isEmpty()){
flag = false;
break;
}
else if(a.peek()=='('){
a.pop();
}
else{
flag = false;
break;
}
}
else if(s1.charAt(i)==']'){
if(a.isEmpty()){
flag = false;
break;
}
else if(a.peek()=='['){
a.pop();
}
else{
flag = false;
break;
}
}
else if(s1.charAt(i)=='}'){
if(a.isEmpty()){
flag = false;
break;
}
else if(a.peek()=='{'){
a.pop();
}
else{
flag = false;
break;
}
}
else{
continue;
}
}
if(flag && a.isEmpty()) System.out.println("YES");
else System.out.println("NO");
}
}
}