Valid Parentheses
【Leetcode 20】
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
public boolean isValid(String s) {
if (s == null || s.length() == 0 || s.length() % 2 != 0) return false;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == '[' || ch == '(' || ch == '{') {
stack.push(ch);
} else if (ch == ']' && !stack.isEmpty() && stack.peek() == '[') {
stack.pop();
} else if (ch == ')' && !stack.isEmpty() && stack.peek() == '(') {
stack.pop();
} else if (ch == '}' && !stack.isEmpty() && stack.peek() == '{') {
stack.pop();
} else {
return false;
}
}
return stack.isEmpty();
}