public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
} else if (c == ')' || c == '}' || c == ']') {
if (stack.isEmpty()) {
return false;
}
char tmp = stack.pop();
switch (c) {
case ')':
if (tmp != '(') {
return false;
}
break;
case '}':
if (tmp != '{') {
return false;
}
break;
case ']':
if (tmp != '[') {
return false;
}
break;
default: break;
}
}
}
if (!stack.isEmpty()) {
return false;
}
return true;
}
}