-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStack.java
33 lines (27 loc) · 993 Bytes
/
Stack.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
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input=sc.next();
//Complete the code
Deque<Character> stack = new ArrayDeque<>();
if(input.length() % 2 != 0)
System.out.println("false");
else{
for(int i = 0; i < input.length(); i++){
if(stack.isEmpty())
stack.addFirst(input.charAt(i));
else{
if((input.charAt(i) == ')' && stack.peek() == '(') || (input.charAt(i) == '}' && stack.peek() == '{') || (input.charAt(i) == ']' && stack.peek() == '['))
stack.pop();
else
stack.addFirst(input.charAt(i));
}
}
System.out.println(stack.isEmpty() ? "true" : "false");;
}
}
}
}