class Solution {
public boolean isValid(String s) {
Stack <Character> stack = new Stack <Character>(); // create empty stack
// traverse through each character
for (char c: s.toCharArray()) {
if (c == '(') {
stack.push(')');
} else if (c == '{') {
stack.push('}');
} else if (c == '[') {
stack.push(']');
// if opening brace does not match closing brace
// or if there is no closing brace
} else if (stack.isEmpty() || stack.pop() != c) {
return false;
}
}
// return null
return stack.isEmpty();
}
}