Skip to content

Commit 3293b00

Browse files
authored
Create ValidParenthesis
1 parent 12b360e commit 3293b00

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
if(s.length()%2!=0)
4+
return false;
5+
Stack <Character> stk=new Stack<Character>();
6+
for(char c:s.toCharArray()){
7+
if(c=='('||c=='{'||c=='['){
8+
stk.push(c);}
9+
10+
else if(c==')'&&!stk.isEmpty()&&stk.peek()=='(')
11+
stk.pop();
12+
13+
else if(c==']'&&!stk.isEmpty()&&stk.peek()=='[')
14+
stk.pop();
15+
16+
else if(c=='}'&&!stk.isEmpty()&&stk.peek()=='{')
17+
stk.pop();
18+
else
19+
return false;
20+
21+
22+
23+
}
24+
return (stk.isEmpty());
25+
}
26+
}

0 commit comments

Comments
 (0)