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.
code
public class ValidParentheses {
public boolean isValid(String s) {
if (s == null || s.length() == 0)
return false;
Stack<Character> stack = new Stack<Character>();
for (Character c : s.toCharArray()) {
if ("([{".contains(String.valueOf(c))) {
stack.push(c);
} else {
if (!stack.isEmpty() && isValid(stack.pop(), c)) {
;
}
return false;
}
}
return stack.isEmpty();
}
public boolean isValid(char c1, char c2) {
return (c1 == '(' && c2 == ')') || (c1 == '[' && c2 == ']')
|| (c1 == '{' && c2 == '}');
}
}