We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2c75c1a commit 128600aCopy full SHA for 128600a
1 file changed
valid-parentheses/juhui-jeong.java
@@ -0,0 +1,22 @@
1
+class Solution {
2
+ public boolean isValid(String s) {
3
+ Stack<Character> stack = new Stack<>();
4
+
5
+ for (char c: s.toCharArray()) {
6
+ if (c == '(' || c == '{' || c == '[') {
7
+ stack.push(c);
8
+ } else {
9
+ if (stack.isEmpty()) {
10
+ return false;
11
+ }
12
13
+ char top = stack.pop();
14
15
+ if (c == ')' && top != '(') return false;
16
+ if (c == '}' && top != '{') return false;
17
+ if (c == ']' && top != '[') return false;
18
19
20
+ return stack.isEmpty();
21
22
+}
0 commit comments