-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaStack.java
More file actions
43 lines (33 loc) · 1.43 KB
/
Copy pathJavaStack.java
File metadata and controls
43 lines (33 loc) · 1.43 KB
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
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
outerloop:
while (sc.hasNext()) {
Stack<Character> stack;
stack = new Stack<>();
String input=sc.next();
char[] inputArr = input.toCharArray();
for (char c : inputArr) {
if (c == '(' || c == '{' || c == '[')
stack.push(c);
else if (c == ')' && !stack.isEmpty())
{
if (stack.peek() == '(')
stack.pop();
}
else if (c == ']' && !stack.isEmpty()) {
if (stack.peek() == '[')
stack.pop();
}
else if (c == '}' && !stack.isEmpty()) {
if (stack.peek() == '{')
stack.pop();
}
else if ((c == ')' || c == '}' || c == ']') && stack.empty()) { System.out.println("false"); continue outerloop; }
}
System.out.println(stack.isEmpty());
}
}
}