-
Notifications
You must be signed in to change notification settings - Fork 481
/
0020.java
43 lines (34 loc) · 1.05 KB
/
0020.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
43
package leetcodeChecks;
import java.util.*;
public class oo2o {
class Solution {
public boolean isValid(String s) {
if(s.isEmpty()){
return true;
}
Stack<Character> st = new Stack<Character>();
for(int i=0; i < s.length();i++){
if(s.charAt(i)=='(' || s.charAt(i)=='[' ||s.charAt(i)=='{'){
st.push(s.charAt(i));
}
else{
if(st.isEmpty()){
return false;
}
char c= st.peek();
if(s.charAt(i)==')' && c=='('|| s.charAt(i) == ']' && c=='['|| s.charAt(i) =='}' && c=='{'){
st.pop();
}else{
return false;
}
}
}
if(st.isEmpty()){
return true;
}
else{
return false;
}
}
}
}