-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
50 lines (48 loc) · 1.54 KB
/
Solution.java
File metadata and controls
50 lines (48 loc) · 1.54 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
41
42
43
44
45
46
47
48
49
50
// https://www.hackerrank.com/challenges/java-stack/problem
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
Stack<String> stack = new Stack<>();
while (sc.hasNext()) {
boolean result = true;
stack.clear();
String input=sc.next();
//Complete the code
char[] a = input.toCharArray();
int i = 0;
loop:
while (i < a.length) {
switch (a[i]) {
case '[': stack.push("["); break;
case '{': stack.push("{"); break;
case '(': stack.push("("); break;
case ']':
if (!stack.pop().equals("[")) {
result = false;
break loop;
}
break;
case ')':
if (!stack.pop().equals("(")) {
result = false;
break loop;
}
break;
case '}':
if (!stack.pop().equals("{")) {
result = false;
break loop;
}
break;
default:
break;
}
i++;
}
if (stack.size() != 0) result = false;
System.out.println(result);
}
}
}