-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
JavaStack.java
42 lines (39 loc) · 1.12 KB
/
JavaStack.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
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input=sc.next();
Stack s = new Stack();
String flag="true";
label:
for(int i=0;i<input.length();i++){
char c=input.charAt(i);
if(c=='{'||c=='('||c=='[')
s.push(c);
else if(c=='}'){
if(s.isEmpty() || (char)s.pop()!='{'){
flag="false";
break label;
}
}
else if(c==']'){
if(s.isEmpty() || (char)s.pop()!='['){
flag="false";
break label;
}
}
else if(c==')'){
if(s.isEmpty() || (char)s.pop()!='('){
flag="false";
break label;
}
}
}
if(!s.isEmpty())
flag="false";
System.out.println(flag);
}
}
}