Skip to content

Commit 55b2990

Browse files
committed
Add valid parentheses Solution - s0ooo0k
1 parent a61ca49 commit 55b2990

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/s0ooo0k.java

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+
Deque<Character> stack = new ArrayDeque<>();
4+
5+
for(char c : s.toCharArray()) {
6+
if(c == '(' || c=='{' || c=='[') {
7+
stack.push(c);
8+
}
9+
if(c == ')') {
10+
if(stack.isEmpty() || stack.peek() != '(') return false;
11+
stack.pop();
12+
}
13+
if(c == '}') {
14+
if(stack.isEmpty() || stack.peek() != '{') return false;
15+
stack.pop();
16+
}
17+
if(c == ']') {
18+
if(stack.isEmpty() || stack.peek() != '[') return false;
19+
stack.pop();
20+
}
21+
}
22+
return stack.isEmpty();
23+
}
24+
}
25+
26+

0 commit comments

Comments
 (0)