-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrackets.java
More file actions
38 lines (35 loc) · 1.03 KB
/
Brackets.java
File metadata and controls
38 lines (35 loc) · 1.03 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
'''
Author: Sreenath T V
https://codility.com/demo/results/trainingSNY2B4-QFV/
'''
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Deque<String> stack = new ArrayDeque<String>();
if (S.length() == 0) {
return 1;
}
String [] temp = new String[S.length()];
temp = S.split("");
for (int i = 0; i < temp.length; i++) {
if (stack.isEmpty()) {
stack.push(temp[i]);
continue;
}
if (temp[i].equals("}") && stack.peek().equals("{")) {
stack.pop();
} else if (temp[i].equals("]") && stack.peek().equals("[")) {
stack.pop();
} else if (temp[i].equals(")") && stack.peek().equals("(")) {
stack.pop();
} else {
stack.push(temp[i]);
}
}
if (stack.isEmpty()) {
return 1;
}
return 0;
}
}