Skip to content

Commit 2727d5f

Browse files
added Valid Parentheses Solution
1 parent faefda7 commit 2727d5f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Topic/Solution/validParentheses.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
if (s.length() % 2 != 0) return false;
4+
Stack<Character> stack = new Stack<>();
5+
for (int i = 0; i < s.length(); i++) {
6+
char c = s.charAt(i);
7+
if (c == '(') {
8+
stack.push(')');
9+
} else if (c == '[') {
10+
stack.push(']');
11+
} else if (c == '{') {
12+
stack.push('}');
13+
} else if (stack.isEmpty() || stack.pop() != c) return false;
14+
}
15+
return stack.isEmpty();
16+
}

0 commit comments

Comments
 (0)