Skip to content

Commit b002fbe

Browse files
authored
0020VALIDPARENTHESIS.java
1 parent 297d171 commit b002fbe

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

0020VALIDPARENTHESIS.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
Stack<Character> st = new Stack<>();
4+
5+
for(int i= 0 ; i<s.length();i++){
6+
char ch= s.charAt(i);
7+
8+
if (ch=='('|| ch=='[' || ch=='{'){
9+
st.push(ch);
10+
11+
}
12+
else{
13+
if(st.isEmpty()){
14+
return false;
15+
16+
}
17+
else if(ch==']' && st.peek()!='['){
18+
return false;
19+
20+
}
21+
22+
else if(ch=='}' && st.peek()!='{'){
23+
return false;
24+
25+
}
26+
else if(ch==')' && st.peek()!='('){
27+
return false;
28+
29+
}
30+
31+
32+
33+
st.pop();
34+
35+
36+
}
37+
38+
}
39+
if(st.isEmpty()){
40+
return true;
41+
42+
}
43+
else {
44+
45+
46+
return false;
47+
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)