Skip to content

Commit 733fceb

Browse files
authored
Create ValidParentheses.java
1 parent f0df99c commit 733fceb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Stacks/ValidParentheses.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//LeetCode 20. Valid Parentheses
2+
//Question - https://leetcode.com/problems/valid-parentheses/
3+
4+
class Solution {
5+
public boolean isValid(String s) {
6+
Stack<Character> stk = new Stack<>();
7+
8+
for(char c : s.toCharArray()){
9+
if(c == '(' || c == '[' || c == '{') stk.push(c);
10+
else{
11+
switch(c){
12+
case ')': if(stk.isEmpty() || stk.pop() != '(') return false;
13+
break;
14+
case ']': if(stk.isEmpty() || stk.pop() != '[') return false;
15+
break;
16+
case '}': if(stk.isEmpty() || stk.pop() != '{') return false;
17+
break;
18+
}
19+
}
20+
}
21+
if(stk.isEmpty()) return true;
22+
return false;
23+
}
24+
}

0 commit comments

Comments
 (0)