Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions valid-parentheses/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Stack;

// TC: O(n)
class Solution {
Stack<Character> st = new Stack<>();
public boolean isValid(String s) {
for(char c : s.toCharArray()) {
if(st.empty() || !isClosing(c)) {
st.push(c);
continue;
}
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack에 쌓아야 하는 조건들을 먼저 검사해서 for-loop 횟수를 최소화 하는 접근이 인상적이네요~~!


char temp = st.peek();
if(temp == '(' && c == ')') {
st.pop();
} else if(temp == '[' && c == ']') {
st.pop();
} else if(temp == '{' && c == '}') {
st.pop();
} else {
st.push(c);
}
}

return st.empty();
}

public boolean isClosing(char c) {
if(c == ')' || c == ']' || c == '}') return true;

return false;
}
}