-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA141.java
37 lines (30 loc) · 1.05 KB
/
A141.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
import java.util.Scanner;
import java.util.Stack;
public class A141 {
static boolean esBalanceado(char[] entrada) {
Stack<Character> pila = new Stack<>();
for (char c : entrada) {
if (c == '(' || c == '[' || c == '{') {
pila.push(c);
} else if (c == ')' || c == ']' || c == '}') {
if (pila.empty()) {
return false;
}
if ( c == ')' && pila.peek() == '(') {
pila.pop();
} else if (c == ']' && pila.peek() == '[') {
pila.pop();
} else if (c == '}' && pila.peek() == '{')
pila.pop();
}
}
return pila.empty();
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
char[] entrada = sc.nextLine().toCharArray();
System.out.println(esBalanceado(entrada)?"YES":"NO");
}
}
}