Skip to content

Commit 0d5b434

Browse files
add: Valid Parentheses
1 parent 634e572 commit 0d5b434

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

valid-parentheses/YoungSeok-Choi.java

+33
Original file line numberDiff line numberDiff line change
@@ -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+
st.pop();
18+
} else if(temp == '{' && c == '}') {
19+
st.pop();
20+
} else {
21+
st.push(c);
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

Comments
 (0)