class Solution {
public boolean isValid(String s) {
if (s.length() == 0 || s == null) {
return true;
}
if(s.length() % 2 == 1){
return false;
}
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
stack.add(s.charAt(i));
} else if (s.charAt(i) == ')') {
if (stack.size() == 0 || stack.pop() != '(') {
return false;
}
} else if (s.charAt(i) == ']') {
if (stack.size() == 0 || stack.pop() != '[') {
return false;
}
} else {
if (stack.size() == 0 || stack.pop() != '{') {
return false;
}
}
}
if (stack.size() != 0) {
return false;
} else {
return true;
}
}
}