We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 634e572 commit 0d5b434Copy full SHA for 0d5b434
valid-parentheses/YoungSeok-Choi.java
@@ -0,0 +1,33 @@
1
+import java.util.Stack;
2
+
3
+// TC: O(n)
4
+class Solution {
5
+ Stack<Character> st = new Stack<>();
6
+ public boolean isValid(String s) {
7
+ for(char c : s.toCharArray()) {
8
+ if(st.empty() || !isClosing(c)) {
9
+ st.push(c);
10
+ continue;
11
+ }
12
13
+ char temp = st.peek();
14
+ if(temp == '(' && c == ')') {
15
+ st.pop();
16
+ } else if(temp == '[' && c == ']') {
17
18
+ } else if(temp == '{' && c == '}') {
19
20
+ } else {
21
22
23
24
25
+ return st.empty();
26
27
28
+ public boolean isClosing(char c) {
29
+ if(c == ')' || c == ']' || c == '}') return true;
30
31
+ return false;
32
33
+}
0 commit comments