forked from the-moonLight0/Hactober-fest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses.java
39 lines (32 loc) · 917 Bytes
/
ValidParentheses.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
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack();
int i=0;
while(i<s.length()){
char ch=s.charAt(i);
if(ch=='{'||ch=='('||ch=='['){
st.push(ch);
}
else if(st.size()>0){
if(ch=='}' && st.peek()=='{'){
st.pop();
}
else if(ch==')' && st.peek()=='('){
st.pop();
}
else if(ch==']' && st.peek()=='['){
st.pop();
}
else{
return false;
}
}
else{
return false;
}
i++;
}
if(st.size()!=0)return false;
return true;
}
}