-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.java
30 lines (24 loc) · 895 Bytes
/
solution.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
class Solution {
public boolean isValid(String s) {
int top = -1;
char[] stack = new char[s.length()];
for(int i=0; i < s.length(); i++){
char curr = s.charAt(i);
if(curr == '(' || curr == '{' || curr == '['){
stack[++top] = curr; //Push element
} else {
if(top < 0)
return false;
char topC = stack[top];
boolean cond1 = topC == '(' && curr == ')';
boolean cond2 = topC == '{' && curr == '}';
boolean cond3 = topC == '[' && curr == ']';
if(cond1 || cond2 || cond3)
--top; // Pop element
else
return false;
}
}
return top < 0;
}
}